liveBook · Manning

Function argument

As explained in Secrets of the JavaScript Ninja, Third Edition

A value supplied to a function call for binding to a corresponding parameter.

An argument is a value supplied when a function is invoked; a parameter is the variable named in the function definition. Arguments are assigned to parameters in order. section 5.4

When the argument and parameter lists differ:

  • Extra arguments are not bound to parameter names; parameters without supplied arguments evaluate as undefined. section 5.4 section 5.6
  • Rest parameters collect remaining arguments, while default parameters supply values when none are provided. section 5.6

Extra arguments

With `function practice(ninja, weapon, technique) {}`, the call `practice("Yoshi", "sword", "shadow sword", "katana")` binds the first three values to the named parameters; `"katana"` is extra. section 5.4

A function can be an argument

`useless(getText)` passes a function as an argument. Inside `useless`, calling `ninjaCallback()` invokes that function. section 5.2.2

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

Use the figure to see positional assignment and what happens to an excess argument. section 5.4

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Defines arguments and explains positional binding, unmatched values, missing values, and rest and default parameters.
Examples
Uses a call with excess string arguments and a callback function passed to another function.
What you can do after
Apply positional binding by predicting which parameters receive values and how unmatched arguments are handled.
How it connects
Connects arguments to function parameters, callbacks, rest parameters, default parameters, and TypeScript call validation.

Also explained in

Compare how 6 books explain function argument →

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.