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. Required fields are marked *

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
JavaScript Array slice()

JavaScript Array slice()

The slice() method in JavaScript returns a shallow copy of a portion of an array as a new array. The original array remains unchanged. slice() Example slice() Syntax The syntax…
View Post
JavaScript String split()

JavaScript String split()

In this tutorial, we will learn about the JavaScript String split() method with examples. The JavaScript String split() method splits a string into an ordered list of strings and returns…
View Post
JavaScript String startsWith()

JavaScript String startsWith()

In JavaScript, the startsWith() method determines whether the given string begins with a specific sequence of characters. Let’s take a look at syntax and examples of the Javascript string startsWith()…
View Post
JavaScript String slice()

JavaScript String slice()

In this tutorial, we will learn about the JavaScript String slice() method with examples. The JavaScript String slice() method extracts a section of the string and returns it as a…
View Post
JavaScript String toUpperCase()

JavaScript String toUpperCase()

In this tutorial, we will learn about the JavaScript String toUpperCase() method with examples. The JavaScript String toUpperCase() method converts the string into uppercase and returns a new string. toUpperCase()…
View Post
JavaScript String endsWith()

JavaScript String endsWith()

In JavaScript, the endsWith() method determines whether the given string ends with a specific sequence of characters. Let’s take a look at syntax and examples of the Javascript string endsWith()…
View Post