Open In App

What is the inline function in JavaScript ?

Last Updated : 13 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, an inline function is a special type of anonymous function that is assigned to a variable, or in other words, an anonymous function with a name. JavaScript does not support the traditional concept of inline functions like C or C++. Thus anonymous function and inline function is practically the same. Unlike normal functions, they are created at runtime.

Syntax:

  • Function:
function func() {
//Your Code Here
}
  • Anonymous function:
function() {
//Your Code Here
}
  • Inline function
let 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 1: In this example, we are showing different types of inline functions that are taking input as a parameter.

Javascript




// Inline function in JavaScript
let sum = function (x, y) {
    console.log(x + y);
}
// An annonymous function
let product = (x, y) => { console.log(x * y) }
 
// Normal function
 
function maxElement(x, y) {
    if (x > y) console.log(`${x} ix the bigger`);
    else console.log(`${y} is the bigger`);
}
 
sum(10, 20);
product(2, 4);
maxElement(23, 45);


Output

30
8
45 is the bigger

Example 2:  In this example, we are showing different types of inline functions.

Javascript




// Normal function
function func() {
    console.log("Hello I'm inside function");
}
 
//anonymous function
let annonymous = function () {
    console.log("Hello I'm inside anonymous function");
}
 
//inline function
let inline_func = function () {
    console.log("Hello I'm inside inline function");
};
 
func();
annonymous();
inline_func();


Output

Hello I'm inside function
Hello I'm inside anonymous function
Hello I'm inside inline function


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads