Learn JavaScript
❯ functions.function_params

Parameters: Defaults & Rest in JavaScript

$concept

Parameters default cleanly (function greet(name = "guest")) — defaults apply on undefined (including omitted) but not on null, and can reference earlier parameters. Rest (...args) gathers surplus arguments into a true array, replacing the array-like arguments object everywhere modern.

Destructured parameters (function f({x, y} = {})) unpack objects at the signature with per-field defaults — the standard options-object pattern. Together these three features eliminated most manual argument plumbing.

$how it works
1

Defaults for undefined

function f(n = 10) uses 10 when n is omitted or undefined — but keeps an explicitly passed null.

2

Rest gathers surplus

function sum(...nums) collects extras into a real array — map/filter/reduce work immediately, unlike arguments.

3

Destructure options

function connect({host = "x", port = 80} = {}) unpacks config objects with defaults at every level.

4

Defaults see earlier params

function f(a, b = a * 2) — later defaults may reference earlier parameters left to right.

$example
function sum(...nums) {
return nums.reduce((t, n) => t + n, 0);
}
console.log(sum(1, 2, 3));
console.log(sum());
Output
6
0
1Rest gathers 1, 2, 3 into a true array [1, 2, 3].
2reduce folds to 6; the empty call folds [] to the seed 0.
3Any arity works — zero through many — from one signature.
$use cases
Options objects with per-field defaults in library APIs
Variadic utilities (sum, merge, log) via rest parameters
Distinguishing omitted (default applies) from null (kept) arguments
Replacing arguments with rest in all new code

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.