Anonymous Function in JavaScript

The term 'anonymous' refers to something that lacks identification or is unknown. In the context of JavaScript, an anonymous function is a function that does not possess a name, essentially being nameless. When we define an anonymous function, it is established without any specific identifier. This distinguishes it from a standard function, which typically has a name. This concept is not exclusive to JavaScript; it is also applicable in numerous other programming languages. The function of an anonymous function remains consistent across different programming environments.

In this segment, we will explore the concept of anonymous functions and their significance within JavaScript. Additionally, we will examine and discuss how to implement them effectively.

Implementation of an Anonymous Function in JavaScript

The following example illustrates the use of an anonymous function and highlights how it differs from a standard function:

Example:

Example

let x = function () {

    console.log('It is an anonymous function');

};

x();

The code presented above demonstrates the use of an anonymous function in which:

  • The purpose of the function is to output a specific message.
  • We utilize the 'function' keyword, which is essential for defining a function in JavaScript, and this function is assigned to a variable named x using 'let'.

The primary emphasis here is that there is no function that we have previously defined. What we have is merely the keyword function followed by parentheses. In the context of a standard function, we typically assign a name to it, as illustrated in the code example below:

Example

function normale() {

  console.log('It is a normal function');

}

normale();

In this instance, we have developed a normale function, which serves as a standard function. This illustrates the distinction between an anonymous function and a traditional function.

Finally, we have invoked the created function.

Thus, this demonstrates the fundamental execution of an anonymous function.

Use of Anonymous Functions in JavaScript

In JavaScript, anonymous functions can serve various functions. Here are a few examples:

  • Utilizing an anonymous function as a parameter for another function
  • Furthermore, we can employ an anonymous function as a parameter to another function. To illustrate this more clearly, let's create a code snippet where we will provide the anonymous function as an argument to another function:
  • Example
    
    setTimeout(function () {
    
        console.log('Execute later after 1 second')
    
    }, 1000);
    

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.

This represents a specific instance of how an anonymous function can be implemented and utilized.

Immediate execution of a function

To call and run a function right after it is defined, the most effective approach is to create an anonymous function. Let's examine an example to clarify how this can be accomplished:

Example

(function() {

    console.log('Hello');

})();

In the code presented above, the anonymous function is executed right away, functioning as outlined in the following manner:

The initial phase involves outlining the function expression, as illustrated in the following example:

Example

(function() {

    console.log('Hello');

})

Once the function is defined, we observe the trailing parentheses accompanied by the terminator (;) that are utilized to call the defined function, as illustrated below:

Example

(function() {

    console.log('Hello');

})();

By doing so, the anonymous function can be executed right away.

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

Here are several applications of an anonymous function, which demonstrates that an anonymous function is defined without a name, can be executed right away, and can serve as an argument in the definition of a standard function.

Input Required

This code uses input(). Please provide values below: