Anonymous Functions in JavaScript

6 min read·Jan 1, 2025

In JavaScript, anonymous functions are functions that are defined without a name. These functions are generally used for short, one-off tasks and are mostly assigned to variables.

To declare an anonymous function, you can use the following syntax:

const variable = function(parameter, ...) {
  instructions
};

To invoke an anonymous functions, you can use the same syntax as for regular functions:

variable(argument, ...);

Note: While anonymous functions help keep functions local to the scope where they are needed, thus avoiding polluting the global scope, they can sometimes make the code harder to read and debug.

Example

Let's consider this script, that stores an anonymous function in a variable:

add.js
const add = function(a, b) {
  return a + b;
}

const sum = add(1, 2);

console.log(sum);

When executed, it will:

  1. Declare a variable named add and initialize it with an anonymous function that returns the sum of its parameters a and b.
  2. Execute the add variable as a regular function and store its return value in the sum variable.
  3. Output the value of the sum variable.

Which will produce this output:

$ node add.js
3

Lambda functions

A lambda function, also called an arrow function, provides a concise syntax for writing short, temporary, anonymous functions without using the function keyword.

const lambda = (parameter, ...) => {
  //
};

Lambda functions also allow you to return "single-line" expressions without explicitly using the return statement.

const lambda = (parameter, ...) => expression;

Note: Parentheses are only mandatory when there are zero or multiple parameters, but can be omitted when there is only one parameter.

Example

Let's consider this function named add:

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

That can also be written using this syntax:

const add = (a, b) => {
  return a + b;
};

Or this syntax:

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

Immediately invoked function expressions

An immediately invoked function expression (IIFE) is a JavaScript design pattern that consists in executing an anonymous function immediately after it's defined.

This type of function creates a private scope, which means that variables declared within the function's body are not accessible from the outside, thus preventing variable collisions with the global scope.

They are mostly used for creating JavaScript modules, allowing encapsulation of functionality within a self-contained scope.

To declare and execute an IIFE, you can use the following syntax:

(function (parameter, ...) {
  instructions
})(argument, ...);

Note: IIFE are by nature anonymous as they are not meant to be explicitly called from anywhere else in the script.

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Anonymous Functions in JavaScript | Backend Brewery