Default, Rest, and Spread Parameters
Default parameter values, rest parameters, and the spread syntax for unpacking an array into a function call — three small but common pieces of syntax.
Default, Rest, and Spread Parameters
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A default parameter value (`function f(limit = 10) {}`) fills in a value only when the argument passed is `undefined` — not for any other falsy value like `0`, `""`, or `null`, which is the detail that trips people up.
- Rest parameters (`function f(...args) {}`) collect every argument from that point onward into a real array, replacing the old `arguments` object, which looks array-like but doesn't have array methods like `.map()`.
- Spread at a call site (`fn(...values)`) does the opposite job: it takes an iterable — an array, a string, anything iterable — and unpacks it into separate arguments for that call, as in `Math.max(...numbers)`.
- Rest and spread share the same `...` syntax but run in opposite directions and opposite places — rest only appears in a parameter list and gathers values in; spread appears at a call site (or in an array/object literal, covered separately) and pushes values out.
- A rest parameter must be the last parameter in the list, because it collects everything remaining in the call — there's no sensible way to have another parameter come after that.
The boilerplate these replace
Before any of this syntax existed, handling “optional” or “variable-length”
arguments meant writing it yourself. A default value meant an if check
inside the function body (if (limit === undefined) limit = 10;). A
variable number of arguments meant reaching for the arguments object —
real, but awkward, since it isn’t a true array and doesn’t come with
.map() or .filter(). And turning an array back into a list of separate
arguments for a call like Math.max() meant .apply() with an ugly
this-as-null convention. Default, rest, and spread parameters are
syntax that does all three of those jobs directly, without the workaround.
Default parameters: a fallback for “nothing was passed”
function charge(amount, taxRate = 0.08) {
return amount * (1 + taxRate);
}
charge(100); // uses 0.08
charge(100, 0.05); // uses 0.05
A default value only kicks in when the corresponding argument is
undefined — which covers both “the caller left this argument out
entirely” and “the caller explicitly passed undefined.” It does not
cover 0, "", false, or null — those are all values someone actually
passed, so the parameter keeps them. This is the single most common mistake
with default parameters: expecting charge(100, 0) to fall back to 0.08
because 0 is falsy. It won’t — 0 is a real, intentional value, not a
signal for “nothing here.”
Rest parameters: gathering the extras into a real array
function sum(...numbers) {
return numbers.reduce((total, n) => total + n, 0);
}
sum(1, 2, 3, 4); // 10
A rest parameter, written ...name as the last item in a parameter list,
scoops up every argument from that position onward into an actual array.
Before rest parameters existed, this required the arguments object —
which exists in every regular function but is only array-like: it has
indexes and a .length, but none of the methods that make arrays useful.
sum above couldn’t call .reduce() directly on arguments without first
converting it. A rest parameter skips that conversion step entirely,
because it’s a real array from the moment the function starts running.
Spread at a call site: unpacking an array into arguments
const scores = [88, 92, 79, 95];
console.log(Math.max(...scores)); // 95
Spread looks identical to rest — same three dots — but runs in the
opposite direction and only shows up where values are being expanded,
not collected. Math.max() takes individual number arguments, not an
array; ...scores unpacks the array into exactly that shape at the call
site, as if you’d typed Math.max(88, 92, 79, 95) by hand. This is the
specific use this lesson covers — spread expanding an iterable into
arguments for a function call. Spread also works inside array and object
literals to build a new collection out of an existing one ([...a, ...b]),
but that’s a distinct use case with its own lesson,
destructuring and spread —
worth knowing the syntax is shared, not worth re-deriving it here.
Telling rest and spread apart
The trick that keeps people from confusing the two: look at where the
... sits. Inside a function’s parameter list, it’s rest, gathering
incoming arguments into an array. At a call site, handing arguments to a
function, it’s spread, expanding an array back out. Same three characters,
opposite jobs, and the context they appear in is the only thing that tells
you which one you’re looking at.
Where this fits
This builds directly on the function syntax from the previous lesson —
default values and rest parameters are properties of a function’s
parameter list, regardless of whether that function is a declaration,
expression, or arrow function. The spread half connects forward to
destructuring and spread,
which picks up the array- and object-literal side of the same ... syntax
once you’ve reached the collections group.
Check yourself
Why does `function greet(name = "friend") {}` still print "friend" if you call `greet(undefined)`, but not if you call `greet(null)` or `greet("")`?
Default parameters only trigger on `undefined` specifically — the signal that an argument was never supplied at all. `null` and `""` are real, deliberately-passed values, so the parameter keeps them rather than silently overriding what you explicitly passed.
What can you do with a rest parameter that you can't do with the old `arguments` object?
Use actual array methods directly on it — `.map()`, `.filter()`, `.reduce()`, and so on — because a rest parameter is built as a real array. `arguments` only looks array-like; it has a `.length` and numeric indexes, but you'd have to convert it to a real array first to chain array methods on it.
What does `Math.max(...numbers)` do differently from `Math.max(numbers)`, given `numbers = [3, 7, 2]`?
`Math.max(numbers)` passes the whole array as one argument, which `Math.max` can't meaningfully compare and returns `NaN` for. `Math.max(...numbers)` spreads the array into three separate arguments — equivalent to `Math.max(3, 7, 2)` — which is what `Math.max` actually needs, since it compares individual values, not a single array.
Why must a rest parameter always come last in a function's parameter list?
A rest parameter collects every remaining argument starting from its position — there's no way to also say "and this one more thing after that," because the rest parameter has already claimed everything from there to the end of the call.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.