liveBook · Manning

Function declaration

As explained in Secrets of the JavaScript Ninja, Second Edition

A JavaScript statement that defines a named function.

Need a named function that stands on its own? A function declaration defines one as a separate JavaScript statement. It begins with `function`, requires a name and parentheses, and encloses its body in braces. Parameters are optional. section 3.3.1

A declaration can live inside another function

`function ninja() { function hiddenNinja() { return "ninja here"; } return hiddenNinja(); }` The inner definition is still a declaration: it stands as its own statement inside the outer function. Scope and identifier resolution for nested functions need further care. section 3.3.1

Placement has a useful twist. When a global or function lexical environment is created, JavaScript first scans it for function declarations, creates their functions, and binds their names. Execution follows, so a call can appear before its declaration. That scan does not descend into other function bodies, and availability depends on the environment; do not assume identical behavior inside blocks. section 5.5.3

Listing 5.9. Accessing a function before its declaration
assert(typeof fun === "function",
  "fun is a function even though its definition isn't reached yet!");   #A

assert(typeof myFunExp === "undefined",                                 #B
  "But we cannot access function expressions");                         #B
                                                                        #B
assert(typeof myArrow === "undefined",                                  #B
  "Nor arrow functions");                                               #B

function fun(){}                                     #C
 
var myFunExpr = function(){};                        #D
var myArrow = (x) => x;                              #D

Compare a declaration available before its definition with function expressions and arrow functions that are not yet available at the same point. section 5.5.3

Keep these distinctions in mind:

  • A function expression is part of another statement, such as an assignment; a declaration stands on its own and must have a name. section 3.3.1
  • An assignment can replace the value associated with a declaration’s name. Early availability does not make that binding permanent. section 5.5.3

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Explains required syntax, nesting, and early identifier registration in lexical environments, while deferring detailed scope and identifier resolution.
Examples
Compares nested declarations with function expressions and tests access to a function before its declaration executes.
What it assumes
Assumes familiarity with JavaScript statements, calls, and assignments, but spells out declaration syntax and the registration process.
What you can do after
Build it from scratch as a named statement and predict when calls before its declaration work in global or function environments.
How it connects
Connects function declarations to function expressions, arrow functions, lexical environments, and identifier binding.

Also explained in

Compare how 6 books explain function declaration →

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.