Parameters: Defaults & Rest in JavaScript
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.
Defaults for undefined
function f(n = 10) uses 10 when n is omitted or undefined — but keeps an explicitly passed null.
Rest gathers surplus
function sum(...nums) collects extras into a real array — map/filter/reduce work immediately, unlike arguments.
Destructure options
function connect({host = "x", port = 80} = {}) unpacks config objects with defaults at every level.
Defaults see earlier params
function f(a, b = a * 2) — later defaults may reference earlier parameters left to right.
function sum(...nums) { return nums.reduce((t, n) => t + n, 0);}console.log(sum(1, 2, 3));console.log(sum());Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.