Anonymous Functions

In JavaScript, we can have data lying around your program as the following in your code.

"test"; [1,2,3]; undefined; null; 1;

It doesn't actually do anything, but the code is valid and is not going to cause an error. This code contains anonymous data-anonymous because the data pieces are not assigned to any variable and therefore don't have a name.

The functions are like any other variable so they can also be used without being assigned a name.

function(a) {
    return a;
}

These anonymous data in your code are not useful, except if they happen to be functions. In this case, there can be two elegant uses for them.

1.You can pass an anonymous function as a parameter to another function. The receiving function can do something useful with the function that you pass. 2.You can define an anonymous function and execute it right away.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents