Anonymous Functions in JavaScript

JavaScript·2 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;}

Unlock the program 🚀

Pay once, own it forever.

€79

30-day money-back guarantee

  • 13 modules
  • 113 lessons with full-code examples
  • 29 projects with commented solutions
  • All future lesson and project updates
  • Lifetime access

By submitting this form, you agree to the Terms & Conditions and Privacy Policy.