What is the inline function in JavaScript ?
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 func() {
//Your Code Here
}
function() {
//Your Code Here
}
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
let sum = function (x, y) {
console.log(x + y);
}
let product = (x, y) => { console.log(x * y) }
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
function func() {
console.log( "Hello I'm inside function" );
}
let annonymous = function () {
console.log( "Hello I'm inside anonymous 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
Last Updated :
13 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...