Skip to content
UniCircle
EasyConceptualArrays

Array and object destructuring

Question JS-ARR-005

What is destructuring? Show array destructuring with skipping and defaults, and object destructuring with renaming.

Show the explanation

Model answer. Destructuring unpacks values from arrays or properties from objects into variables in one statement.

JavaScript
// arrays: by position
const [first, , third = 'none'] = ['a', 'b'];   // first = 'a', third = 'none'
let x = 1, y = 2;
[x, y] = [y, x];                                // swap without a temp variable

// objects: by name
const { name, city: hometown = 'Unknown' } = { name: 'Meena', city: 'Kakinada' };
// name = 'Meena', hometown = 'Kakinada'

// in parameters
function greet({ name, role = 'student' }) { return `${name} (${role})`; }

Defaults apply only when the value is undefined. Destructuring null or undefined itself throws, so give the parameter a default when it may be missing: function f({ a } = {}).

Original UniCircle question. Last reviewed 22 Oct 2026.