liveBook · Manning

Default value

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A function parameter form that supplies an automatic fallback value when the caller does not provide an argument or provides undefined.

Suppose a chart is usually 800 pixels wide, but callers sometimes need a smaller one. A default parameter puts the usual value in the function signature: `function approvalsChart(ratings, width = 800, height = width / 2) { /* ... */ }`. A default applies when its argument is omitted or is `undefined`; a supplied value takes its place. section 15.1

Follow the chart sizes

Call `approvalsChart(nationalRatings)` and `width` becomes 800; `height` is then calculated as 400. Call `approvalsChart(georgiaRatings, 400)` and the supplied width makes the default height 200. The height expression can use `width` because it appears earlier in the parameter list. section 15.1

Three rules make defaults easier to use:

  • A default expression is evaluated when needed, not fixed when the function is written. In an object method, it can read `this`: changing `ajax.host` changes the URL produced by a later `ajax.load()` call without an argument. section 15.1
  • Defaults belong to parameter positions. With `function range(min = 0, max)`, `range(10)` sets `min` to 10; it does not move 10 into `max`. Put required parameters before optional ones instead of expecting arguments to shift. section 15.1
  • Defaults handle expected parameters. A rest parameter serves a different job: gathering a variable number of remaining arguments into an array. section 15.2 section 15.5
Listing 15.5. Calling getRatings only once
const chartManager = { getRatings() { // some intensive task to calculate all the ratings }, nationalRatings(ratings = this.getRatings()) { return approvalsChart(ratings) }, stateRatings(state, ratings = this.getRatings()) { stateRatings = ratings.filter(function(rating) { rating.state === state }) return approvalsChart(ratings, 400) }, stateAndNationalRatings(state) { const ratings = this.getRatings() const nationalChart = this.nationalRatings(ratings) const stateChart = this.stateRatings(state, ratings) return { nationalChart, stateChart } } } const charts = stateAndNationalRatings('georgia')

When each chart method is called alone, its ratings parameter defaults to `this.getRatings()`. When both charts are needed, their caller calculates ratings once and passes that result to each method, avoiding the second calculation. section 15.2

Defaults can also fill missing object settings, but that is a different operation: `Object.assign({ width: 900, height: 500 }, options)` builds map options by copying properties, with later objects overriding earlier ones. For practice, give a `car` function a default seat count, then use a rest parameter in its `board` method to handle passengers who fit and those who do not. section 10.1 section 15.5

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Works through dependent expressions, invocation-time context, positional behavior, and avoiding repeated computation; it does not develop a general argument-shifting mechanism.
Examples
Uses approval-rating charts, a changing AJAX host, a one-argument range call, and shared ratings calculations.
What it assumes
Assumes familiarity with JavaScript function calls and object methods while introducing default-parameter syntax through examples.
What you can do after
Build it from scratch: write function defaults that depend on earlier parameters and pass precomputed values to skip redundant work.
How it connects
Connects default parameters to function arguments, object methods, `this`, rest parameters, and `Object.assign` defaults for object properties.

Also explained in

Compare how 20 books explain default value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.