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





