Computer >> Computer tutorials >  >> Programming >> Javascript

What is an anonymous function in JavaScript?


A function expression is similar to and has the same syntax as a function declaration One can define "named" function expressions (where the name of the expression might be used in the call stack for example) or "anonymous" function expressions.

An example of an anonymous function expression (the name is not used) −

var myFunction = function() {
   // Function code
}

This function can be invoked using the variable name referring to it −

myFunction()

In conclusion, an anonymous function is a function that is not stored but is associated with a variable. Anonymous functions can accept inputs and return outputs, just as standard functions