How can we assign a function to a variable in JavaScript?

JavascriptWeb DevelopmentFront End Technology

In JavaScript, you can assign a function in a variable using the concept of anonymous functions. Anonymous, as the name suggests, allows creating a function without any names identifier. It can be used as an argument to other functions. They are called using the variable name −

This is how JavaScript anonymous functions can be used −

var func = function() {
   alert(‘This is anonymous');
}
func();

Anonymous functions are always loaded using a variable name. Anonymous, as the name suggests, allows creating a function without any names identifier. It can be used as an argument to other functions. Call them using a variable name −

Here’s an example −

//anonymous function
var a = function() {
   return 5;
}
raja
Published on 23-Feb-2018 08:06:21
Advertisements