JavaScript Anonymous Functions

Total
0
Shares

An anonymous function is a type of function that has no name. In simple terms, we can call it a function without a name. 

Anonymous functions are not accessible after the initial creation it can only be accessed by a variable stored as a function value. 

An anonymous function can contain multiple arguments, but it can have only one expression.

Anonymous Functions Syntax 

Like any other standard function, an anonymous function can accept input parameters and return output.

function() {
    // Function Body
 }

Example 1: Anonymous Functions in JavaScript

The following example is the demonstration of a simple anonymous function in JS. 

The function does not have any name associated with it, and since we need to call this anonymous function later, we need to assign this to a variable to call it later.

We can call the anonymous function by invoking the variable text() as shown below.

let text = function () {  
    console.log('Hello World');  
};  

text();

Output

Hello World

Example 2: Anonymous Functions with arguments in JavaScript

In this example, let’s take a look at how to pass arguments to the anonymous function.

let printName = function (name) {  
    console.log('Hello ',name);  
};  

printName('Chandler Bing');

Output

Hello  Chandler Bing

Example 3: Using anonymous functions as arguments of other functions

We can pass an anonymous function to another function as its argument as JavaScript supports higher-order functions. Let us take a look at a simple use case where we achieve this functionality.

setTimeout(function () {
    console.log('Welcome to JavaScript world')
}, 1000);

Output

Welcome to JavaScript world

We pass an anonymous function as an argument to the setTimeout() method in the above code. 

The setTimeout() method will execute the anonymous function after a timeout of 1 second.

Example 4: Immediate execution of a function

If you want to invoke and execute a function immediately after its declaration, creating an anonymous function is the best way. Let’s take a look at the example of how we can achieve this functionality.

(function () {
    console.log('Welcome to JavaScript world')
})();

Output

Welcome to JavaScript world

Suppose you want to execute the anonymous function immediately, you need to wrap the entire function using parenthesis. Also, add the trailing parenthesis at the end to call the function as shown in the above code. 

Sometimes, you need to pass the argument to the function and execute it immediately. Below is an example of passing an argument and executing the anonymous function immediately.

(function () {
    console.log('Hello ' + fname + lname)
})(fname = "Chandler ", lname = "Bing");

Output

Hello Chandler Bing

Example 5: Arrow functions

ES6 has introduced an arrow function that provides a shorthand expression to declare an anonymous function.

If you look at the below example, we have eliminated the function keyword and used an arrow to shorthand the entire function.

Example

let text = () => console.log('Hello World'); 
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like
JavaScript String toLowerCase()

JavaScript String toLowerCase()

Table of Contents Hide toLowerCase() ExampletoLowerCase() SyntaxtoLowerCase() ParametertoLowerCase() Return ValueExample: JavaScript Convert string from uppercase to lowercase In this tutorial, we will learn about the JavaScript String toLowerCase() method with…
View Post
JavaScript String split()

JavaScript String split()

Table of Contents Hide split() Examplesplit() Syntaxsplit() Parametersplit() Return ValueExample 1: JavaScript split() method Example 2: JavaScript Split the string into an array of characters Example 3: JavaScript Splitting a…
View Post