Destructuring in JavaScript
Destructuring unpacks values at declaration: const {name, age} = user pulls fields into locals; const [first, second] = pair unpacks arrays positionally. Defaults (const {role = "user"} = ...) and renaming ({name: displayName}) handle absence and collisions, and rest (...rest) collects leftovers.
Function parameters destructure too — ({host, port = 80} = {}) is the options-object idiom — and nested patterns unpack depth in one statement. Overused, destructuring obscures origins; used well, it removes forests of .access chains.
Unpack objects by name
const {name, age} = user — locals bound from same-named fields.
Arrays by position
const [first, second] = pair — order-based unpacking with skippable holes ([, second]).
Defaults and renames
{role = "user"} defaults missing; {name: displayName} renames on the way out.
Destructure parameters
function f({x, y} = {}) — options objects with per-field defaults at the signature.
let user = { name: "Ada", age: 36 };let { name, age } = user;console.log(name, age);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.