Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Expressions & Operators  ›  Capstone Project

Default and Rest Parameters

Default Params 12 minA default parameter (param = value) supplies a fallback only when the argument is undefined; a rest parameter (...names) gathers every remaining argument into a real array, and must be the last parameter.

Default and Rest Parameters

In short: function f(x = 50) {} gives x a default value — but that default only kicks in when the caller passes undefined or leaves the argument out entirely; an explicit 0, "", or false is a real value and is used as-is. function f(first, ...rest) {} uses the rest parameter to scoop up every argument after the named ones into a genuine array — it must be the last parameter, since nothing could come after it.

A form with a pre-filled fallback field: play(80) fills volume = 80; play() leaves it blank and falls back to volume = 50 (default only fires for undefined, play(0) keeps 0, not 50); below, a duffel bag: queue("Blue Hour", "Red Dawn", "Green Tide") gives first = "Blue Hour" and rest = ["Red Dawn", "Green Tide"], a real array

Fall back to a default volume

A default only fills in when the argument is genuinely missing — undefined, whether from an omitted argument or an explicit one:

javascript

play(80) prints 80 — the caller supplied a value, so the default is never consulted. play() prints 50 — no argument at all means volume is undefined, which triggers the default. play(0) prints 0, not 50 — 0 is an explicit, real argument, and defaults only ever replace undefined, exactly the same rule ??= uses for variables.

Gather every extra song into a real array

...rest collects whatever arguments are left after the named parameters, packaged as an actual array with every array method available:

javascript

first takes the first argument, "Blue Hour". rest, being the final parameter, absorbs everything after it — ["Red Dawn", "Green Tide"] — as a real array, so rest.length, rest.map(...) and so on all work directly, unlike the older arguments object.

Quick practice

quick drill

What does function play(volume = 50) { console.log(volume); } play(); print?

quick drill

What does function play(volume = 50) { console.log(volume); } play(0); print?

quick drill

In function queue(first, ...rest) {}, what type of value is rest inside the function?

quick drill

What does function queue(first, ...rest) { console.log(rest); } queue("A", "B", "C"); print?

quick drill

Where must a rest parameter appear in a function's parameter list?

🎯 Your turn

Explain what function play(volume = 50) { console.log(volume); } play(0); prints and why, and what function queue(first, ...rest) { console.log(rest); } queue("A", "B", "C"); prints and why.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainZero volume, and extra songs

Explain what function play(volume = 50) { console.log(volume); } play(0); prints and why, and what function queue(first, ...rest) { console.log(rest); } queue("A", "B", "C"); prints and why.