liveBook · Manning

Function declaration

As explained in Secrets of the JavaScript Ninja, Third Edition

A function definition is source code that declares a function and specifies its parameters and executable body.

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

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Specifies required syntax and statement placement, demonstrates nesting, and defers scope resolution and early-call behavior.
Examples
Shows named declarations, including a function declared inside another function.
What it assumes
Assumes familiarity with C-derived function syntax but distinguishes JavaScript declarations from expressions and parameters from arguments.
What you can do after
Build it from scratch using a required name, optional parameters, and a braced body in a standalone statement.
How it connects
Contrasts function expressions and arrow functions while connecting declarations to parameters, arguments, and scope.

Also explained in

Compare how 7 books explain function declaration →

Related concepts

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