Anonymous functions in JavaScript

An anonymous function is a function without a name.

An anonymous function is often not accessible after its initial creation.

An anonymous function can be assigned to a variable.

Here, the anonymous function has no name between the function keyword and parentheses ().

let show = function () {

console. log( ‘Anonymous function ‘);

};

show( );

/ / output: ‘Anonymous function ‘

Anonymous functions can be used as an argument to other functions.

Below, an anonymous function is passed to the set Timeout() function.

The set Timeout() function executes the anonymous function one second later.

setTimeout( function () {

console. log( ‘Execute later after 1 second’)

}, 1000);

If you want to create a function and execute it immediately after the declaration, you can use the anonymous function as IIFE.

(function() {

console. log( ‘IIFE’);

})();

Leave a Reply

Your email address will not be published.