Skip to the workspace
Pebellslearn by building
Functions  ›  Lesson

Default and Rest Parameters

Default Params 12 min

javascript / Default and Rest Parameters

Explore each scene at your own pace.

01 · Default and Rest Parameters

Default and Rest Parameters

Default and Rest Parameters

A default parameter supplies a value when its argument is missing or undefined. A rest parameter collects the remaining arguments into an array.

A default is a missing-value rule, not a replacement for every falsy value.

See the mechanism

Walk through the modelDefault and Rest ParametersAuthored snapshots of the example; use Run below to test the code.
title"Untitled"
tags[]
Only undefined activates the parameter default.
1 of 3

Follow one example

javascript

The first call has no title argument, so it uses "Untitled" and an empty tags array. In the second call, an empty string is a supplied value, so the default is not used. The remaining two arguments become tags, whose length is 2. A rest parameter must come last.

Predict, change and check

Predict: Will explicitly passing null activate the default title?

Test your explanation: Compare missing, undefined, null and an empty string. Then add a third tag and check that title does not change while tags.length increases.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Missing is narrower than falsy

A parameter default applies when the supplied argument is undefined or omitted, not when it is null, 0, false or empty text. A rest parameter gathers the remaining arguments into an actual array and must be the last parameter. It is not the same construct as object rest or spread at a call site, despite sharing three-dot syntax.

Default expressions are evaluated for the call that needs them; they are not a single shared mutable default object in the way some other languages behave. Earlier parameters can be available to later default expressions. Test an omitted argument separately from an explicitly null argument and from an intentionally empty string.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.