liveBook · Manning

Function expression

As explained in Secrets of the JavaScript Ninja, Second Edition

A function defined as an expression rather than a declaration, producing a function value that can be assigned or passed around.

A function expression creates a function value as part of a statement. Like other values, it can be assigned to a variable or property, passed as an argument, or returned from another function. It need not have a name, though named function expressions are also possible. section 3.3 section 3.3.1 section 3.5

A function in several roles

var myFunc = function(){}; myFunc(function() { return function(){}; }); section 3.3.1

The key contrast with a function declaration is placement: a declaration stands as a separate statement, while an expression belongs within one. Their differences can also affect when a function is available for invocation. section 3.3.1 section 3.3

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Defines expression placement and naming, then flags invocation-availability differences without detailing them here.
Examples
Uses assignment to a variable, passing a function as an argument, returning a function, and immediate invocation.
What you can do after
Understand function expressions as values that can be assigned, passed as arguments, and returned.
How it connects
Connects function expressions to first-class functions, function declarations, and arrow functions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.