Need a named function that stands on its own? A function declaration defines one as a JavaScript statement: `function` followed by a required name, parentheses containing optional comma-separated parameters, and a body in braces. The body may be empty. section 5.1.1
A declaration and a call
`function greet(name) { return name; }` declares `greet`. In `greet("Ada")`, `name` is the parameter listed in the definition, while `"Ada"` is the argument supplied by the call. JavaScript also permits a declared function to be called before its definition appears; the reason for that behavior is a separate topic. section 5.1.1 section 5.4 section 5.1
Two distinctions prevent common misreadings:
- A declaration is its own statement. A function placed on the right side of an assignment or passed as an argument is a function expression, even if it also uses the `function` keyword. section 5.1.1
- A call need not supply exactly one argument per parameter. Arguments bind to parameters in order; extra arguments receive no parameter name, and parameters without arguments evaluate as `undefined`. section 5.4 section 5.6
Listing 5.1 Examples of function declarations
function samurai() { #A
return "samurai here"; #A
}
function ninja() { #B
function hiddenNinja() { #C
return "ninja here"; #C
} #C
return hiddenNinja();
}
The listing puts one declaration inside another. Nested function declarations are valid; questions about how names resolve across their scopes require a closer look at scope. section 5.1.1
Use a declaration when a named, standalone definition fits the code. For a function defined within an assignment or another expression, use a function expression instead; arrow functions offer another syntax with different `this` behavior. section 5.1.1 section 5.1






