Javatpoint Logo
Javatpoint Logo

JavaScript Anonymous Functions

The meaning of the word 'anonymous' defines something that is unknown or has no identity. In JavaScript, an anonymous function is that type of function that has no name or we can say which is without any name. When we create an anonymous function, it is declared without any identifier. It is the difference between a normal function and an anonymous function. Not particularly in JavaScript but also in other various programming languages also. The role of an anonymous function is the same.

Here, in this section, we will get to know about the anonymous function and its role in JavaScript. We will also learn and discuss its implementation.

Implementation of an Anonymous Function in JavaScript

An example is shown below that will make us understand how to use an anonymous and why it is different from a normal function:


The above code is the implementation of the anonymous function where:

  • The function is created for displaying the message as its output.
  • We have used the function keyword, which is used when we create any function in JavaScript, and the function is assigned to a variable x using 'let'.

The main focused point is that there is no function we have declared before. It is just the keyword function and parenthesis. In the case of a normal function, we use to name the function as shown in the below code example:

Here, we have created a normale () function, which is a normal function. It is the difference between an anonymous function and a normal function.

Finally, we have invoked the created function.

So, in this way the basic implementation of an anonymous function is done.

Use of Anonymous Functions in JavaScript

We can use the anonymous function in JavaScript for several purposes. Some of them are given below:

  • Passing an anonymous function to other function as its argument
  • We can also use an anonymous function as an argument for another function. To understand better, let's implement a code under which we will pass the anonymous function as an argument value for another function:

The above code implements the use of anonymous function as an argument to a new function where:

  • The function setTimeout () will output the anonymous function after a second.
  • We have created an anonymous function and passed it to the setTimeout () as its argument.
  • Inside it, when the code gets executed, it will print the statement after a second of the execution time.

It is one such implementation and use of the anonymous function.

Immediate execution of a function

In order to invoke and execute a function immediately after its declaration, creating an anonymous function is the best way. Let' see an example to understand how we can do so:

In the above code, the anonymous function is invoked immediately where it works as described in the following way:

The first step is to define the function expression, as shown below:

After defining the function, we can see the trailing parenthesis () followed by the terminator (;) that are used for invoking the defined function as shown below:

In this way, the anonymous function can be invoked immediately.

Note: One can also pass the arguments in the function and invoke it too.

These are some uses of an anonymous function that concludes that an anonymous function is the one with no name, can be invoked immediately and can be used as an argument value in a normal function definition.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA