Course contentsShow
← JavaScript
Lesson 382 of 2,72311. FunctionsFree lesson

Anonymous Functions

Learn about functions without names and where they're commonly used.

Anonymous Functions

You'll learn what anonymous functions are and where you'll find them most often in your code.

What Is an Anonymous Function?

An anonymous function is simply a function without a name. While function declarations like function greet() {} have a name (greet), anonymous functions skip the identifier entirely.

You've already learned about function expressions, where you assign a function to a variable:

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

The function itself has no name—it's anonymous. The variable add holds the function, but the function object doesn't have its own identifier.

Where Anonymous Functions Shine

Anonymous functions are extremely common when you pass functions directly as arguments or use them immediately:

// Passed directly (you'll see this pattern later)
setTimeout(function() {
  console.log("This function has no name!");
}, 1000);

Notice the function() {} syntax with no name between function and the parentheses.

Named vs Anonymous

Compare these two function expressions:

// Anonymous
const square = function(x) {
  return x * x;
};

// Named function expression
const cube = function cubeNumber(x) {
  return x * x * x;
};

Both work identically when called (square(5) or cube(3)), but the anonymous version omits the internal name.

Key Takeaway: Anonymous functions are functions without names, commonly used in function expressions and as direct arguments—they're the unnamed workhorses of JavaScript that do their job without needing a formal introduction.