What is the inline function in JavaScript ?
In JavaScript, inline function is a special type of anonymous function which is assigned to a variable, or in other words, an anonymous function with a name. JavaScript does not support the traditional concept of inline function like in C or C++. Thus anonymous function and inline function is practically the same. Unlike normal function, they are created at runtime.
Syntax:
- Function:
function func() { //Your Code Here } - Anonymous function:
function() { //Your Code Here } - Inline function
var func = function() { //Your Code Here };
Explanation:
Making an inline function is really simple. First, make an anonymous function(a function with no name), and then assign it to a variable.
Example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>JS Functions</title> <!-- jQuery CDN --> integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"> </script> <!-- End of CDN --> <style> button { background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; font-size: 16px; margin: 8px 2px; cursor: pointer; display: block; width: 270px; } </style> </head> <body> <h1 style="text-align:center;color:green;">GeeksforGeeks</h1> <p align="center"> <button id="function">function</button> <button id="anonymous-function">anonymous function</button> <button id="inline-function">inline function</button> </p> <script type="text/javascript"> //function function func() { alert("Hello I'm inside function"); } $('#function').click(func); //anonymous function $('#anonymous-function').click(function() { alert("Hello I'm inside anonymous function"); }); //inline function var inline_func = function() { alert("Hello I'm inside inline function"); }; $('#inline-function').click(inline_func); </script> </body> </html> |
Output:

Clicking on any of the above button will call the respective function and and an alert message will show up. Let’s say “inline function” button is clicked then the following alert message will pop up.

Recommended Posts:
- How does inline JavaScript work with HTML ?
- How to write an inline IF statement in JavaScript ?
- How to remove all inline styles using JavaScript/jQuery ?
- How to stop event propagation with inline onclick attribute in JavaScript ?
- What is the difference between inline-flex and inline-block in CSS?
- Inline function in C
- What is the difference between display: inline and display: inline-block in CSS?
- How to write a:hover in inline CSS?
- HTML | Block and Inline Elements
- Type Aliases vs Inline Classes
- How to remove the space between inline-block elements?
- Displaying inline and multiline blocks of code using Bootstrap
- How to call a function that return another function in JavaScript ?
- JavaScript | Array some() function
- JavaScript | Math.pow( ) Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



