Skip to main content

Command Palette

Search for a command to run...

Anonymous function in JavaScript

Published
3 min read
Anonymous function in JavaScript
T

Web developer with a passion for creating user-friendly websites. Proficient in HTML, CSS, Js, and React.js and open to new opportunities .


Today we’re talking about anonymous functions in JavaScript. Anonymous functions are a great way to write concise and reusable code that can be used in multiple places throughout your program. They allow you to create quick, one-off snippets of code without having to define a function name or parameters beforehand. This makes them incredibly useful for situations where you only need the functionality once and don’t want it cluttering up your main script with unnecessary details.

A function without a name is called an anonymous function. How to define an anonymous function is shown below:

(function () {
console.log("how are you")
 })();

You will receive a syntax error if you don't put the anonymous function inside of the (). The anonymous function becomes an expression with a function object return value when the () is used.

Immediately invoked function execution

If you want to create a function and execute it immediately after the declaration, you can declare an anonymous function like this:

(function() {
    console.log('IIFE');
})();

An anonymous function is not accessible after its initial creation. Therefore, you often need to assign it to a variable.

For example, the following shows an anonymous function that displays a message:

let show = function() {
    console.log('Anonymous function');
};
show();

In this example, the anonymous function has no name between the function keyword and parentheses ().

Because we need to call the anonymous function later, we assign the anonymous function to the show variable.

Since the whole assignment of the anonymous function to the show variable makes a valid expression, you don’t need to wrap the anonymous function inside the parentheses ().

Using anonymous functions as arguments:

In practice, you often pass anonymous functions as arguments to other functions. For example:

setTimeout(function() {
    console.log('Execute later after 1 second')
}, 1000);

In this example, we pass an anonymous function into the setTimeout() function. The setTimeout() the function executes this anonymous function one second later.

Note that functions are first-class citizens in JavaScript. Therefore, you can pass a function to another function as an argument.

Arrow functions

ES6 introduced the arrow function expressions that provide a shorthand for declaring anonymous functions:

For example, this function:

let show = function () {
    console.log('Anonymous function');
};

… can be shortened using the following arrow function:

let show = () => console.log('Anonymous function');

Similarly, the following anonymous function:

let add = function (a, b) {
    return a + b;
};

… is functionally equivalent to the following arrow function:

let add = (a, b) => a + b;

Summary

  • Anonymous functions are functions without names.

  • Anonymous functions can be used as an argument to other functions or as an immediately invoked function execution.


I'm glad that it was able to help clear up any confusion you may have had about the Anonymous function in JavaScript. It's always nice when we can take something complicated and break it down into smaller, more manageable pieces.

Thanks again for reading – I hope this information will be useful going forward!