SiteMap  | About    

Javascript anonymous functions

The two most common ways to create a function in javascript are by using the function declaration or function operator . Anonymous functions are created using the function operator. Anonymous functions are used heavily in JavaScript for many things, most notably the many callbacks used by the language's many frameworks. The ECMAScript specification does not have any mention of the term anonymous.

An anonymous function allows a programmer to create a function that has no name . In other words anonymous functions can be used to store a bit of functionality in a variable and pass that piece of functionality around it and created at runtime. Anonymous functions are declared using the function operator instead of the function declaration.

Normal function definition:

In the above script you can see, it creates a function with name "callMe".

Anonymous function definition:

In the above script you can see, it declares an unnamed function and assigns it to a new variable named "callMe".

Here, we can see that these two ways of defining a function are essentially the same; both result in a function being created, and a new variable named "callMe" assigned to the current scope. However, the second function is anonymous. The function operator can be used anywhere that it's valid to use an expression. For example you can use the function operator when a variable is being assigned, when a parameter is being passed to a function or in a return statement. (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.