liveBook · Manning

Function parameter

As explained in Secrets of the JavaScript Ninja, Second Edition

A variable listed in a function definition that receives a corresponding argument when the function is invoked.

A parameter is a variable named in a function definition. When the function is invoked, arguments—the values passed to it—are assigned to parameters in order. section 3.4

Parameters appear across JavaScript function forms:

  • Function declarations and function expressions can list parameters in their definitions. section 3.4
  • Arrow functions use a comma-separated parameter list; parentheses are optional for one parameter but required for none or more than one. section 3.3.2

Definition versus invocation

In skulk(ninja), ninja is the parameter. Calling skulk("Hattori") passes "Hattori" as an argument, assigning that value to ninja. section 3.4

Arguments are matched to parameters by position. Extra arguments are not assigned to named parameters; access to those excess values is deferred. JavaScript permits calls with different numbers of arguments and parameters. section 3.4

Figure 3.7. The difference between function parameters and function arguments

Use the diagram to distinguish a parameter in a definition from an argument in a call. section 3.4

Figure 3.8. Arguments are assigned to function parameters in the order specified. Excess arguments aren’t assigned to any parameters.

The figure illustrates positional assignment and what happens to excess arguments. section 3.4

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Defines parameters, explains positional assignment and excess arguments, and defers how to access excess values.
Examples
Uses skulk and practice calls to demonstrate parameter assignment, and arrow functions to show parameter-list syntax.
What it assumes
Assumes enough object fundamentals to get by and defines the parameter–argument distinction directly.
What you can do after
Understand how function definitions name parameters and how calls assign arguments to them.
How it connects
Connects parameters with function arguments, function declarations, function expressions, arrow functions, and first-class 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.