Glossary / Function expressions

Function expressions

A function expression creates a function value as part of an expression. It can be assigned to a variable, passed as a callback, returned, or invoked immediately. Function expressions may be anonymous or have an internal name that helps recursion and debugging. Their execution availability depends on when the expression evaluates, not merely where its text appears.

Forms and naming

const normalize = function normalizeInput(value) {
  return value.trim().toLowerCase();
};

const format = (value) => `Value: ${value}`; // arrow function expression

The internal name normalizeInput is useful inside the function and in stack traces, but it is not ordinarily an outer binding. An anonymous function assigned to a variable may receive an inferred name for diagnostics, but explicit names can be clearer for complex callbacks. Function expressions are distinct from function declarations: the variable binding controls when the function value can be read.

A function expression can be invoked immediately (IIFE) to create a local scope, though modules and block-scoped declarations have reduced the need for that pattern. Traditional function expressions can be constructors; arrow expressions cannot. The expression form does not determine this behavior by itself: traditional functions derive it from call-site, while arrows capture it lexically.

Choosing a form

Use declarations for stable named operations and expressions for callbacks, local behavior, and values assembled at runtime. Give complex functions meaningful names. Avoid deeply nested anonymous callbacks when named helpers improve stack traces and reviewability.

Interview questions

  1. Declaration versus expression? A declaration creates a binding during instantiation; an expression creates a function value when evaluated.
  2. What is a named function expression name for? Internal self-reference and debugging; it is not generally an outer binding.
  3. Are arrows constructable? No.
  4. Does every function expression have lexical this? No; only arrow functions do.

References