← Back to JavaScript, page 1

javascript-0008 · Easy fresher

What is destructuring in JavaScript, and what can it do beyond picking out values?

Destructuring pulls values out of arrays and objects into separate variables with one short pattern. It supports renaming, default values, nested patterns, skipping items, and collecting the rest. It is often used for function parameters and for swapping variables.

Full answer

Destructuring is a pattern on the left side of an assignment or declaration that mirrors the shape of the data on the right.

Array patterns work by position. const [first, , third] = list skips the second item. Object patterns work by name. const { name, age } = student creates two variables named after the properties. Several extras make the syntax powerful:

  • A new name: { name: studentName } reads the property name into a variable called studentName.
  • A default: { city = 'Pune' } applies only when the value is undefined, not for null.
  • A rest element: [head, ...others] or { id, ...details } collects what is left.
  • A nested pattern: { address: { city } } reaches into a nested object.

Patterns also work in function parameters, which gives named arguments with defaults, and in for...of loops over arrays of objects or over Object.entries. A swap needs no temporary variable: [a, b] = [b, a].

Destructuring null or undefined throws a TypeError, so give a default object when the source may be missing.

javascript
const student = {
  name: 'Kiran',
  marks: { maths: 91, science: null },
  tags: ['topper', 'sports', 'music'],
};

const { name: studentName, city = 'Pune' } = student;
console.log(studentName, city);

const { marks: { maths, science = 50 } } = student;
console.log(maths, science); // science is null, so the default is NOT used

const [firstTag, , thirdTag, ...others] = student.tags;
console.log(firstTag, thirdTag, others);

const { name, ...everythingElse } = student;
console.log(name, Object.keys(everythingElse));

function printCard({ title, level = 'beginner' } = {}) {
  return `${title ?? 'untitled'} (${level})`;
}
console.log(printCard({ title: 'Closures' }));
console.log(printCard());

let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a, b);

try {
  const { x } = null;
} catch (error) {
  console.log(error.name);
}

Run with Node.js v24.22.1 as js-8.js; two runs gave identical output. Output: Kiran Pune 91 null topper music [] Kiran [ 'marks', 'tags' ] Closures (beginner) untitled (beginner) 2 1 TypeError

Key points

  • Array patterns match by position; object patterns match by property name.
  • Rename with name: alias and set defaults with =.
  • Defaults apply only to undefined, not to null.
  • The rest element collects the remaining items or properties.
  • Destructuring null or undefined throws a TypeError.

Common mistakes

  • Expecting a default value to replace null. It only replaces undefined.
  • Starting a statement with an object pattern without parentheses, such as { a } = obj, which is parsed as a block.
  • Destructuring a value that may be null or undefined without a fallback.
  • Over-nesting patterns until the line is harder to read than plain property access.

Follow-ups

  • How do you destructure a value from an object returned by an async function?
  • How does destructuring from a Map or a Set work?
  • What is the difference between a rest element and the spread operator?

Tips

  • Show defaults and renaming; most candidates only show the basic form.
  • Point out the null versus undefined rule for defaults.
  • Mention function parameter destructuring, which is what real codebases use the most.