Back to JavaScript Notes
Topic #360

Destructuring

You will be able to extract values from JavaScript arrays and objects in a single assignment, with renaming, defaults, and rest patterns.

What it is

Destructuring is an assignment pattern that pulls values out of arrays and objects in one step. Instead of writing const name = user.name;, you write const { name } = user;. For arrays, values are matched by position: const [first, second] = list;. Related terms include object destructuring, array destructuring, renaming, default values, and rest properties.

Why it matters

  • It reduces repetitive property access in functions and components.
  • It makes data flow explicit by naming the values you need.
  • It supports defaults, so missing data does not become undefined.
  • It can collect remaining items with ... without loops.
  • It works well with function parameters, such as function greet({ name }).

Syntax or steps

For objects, use curly braces and property names. For arrays, use square brackets and positions. You can rename with property: variable, provide defaults with variable = fallback, and collect leftovers with ...rest.

const { name: userName = "anon" } = user;
const [first, second, ...others] = items;

Example

const user = {
  name: "Ada",
  age: 36,
  address: { city: "London" }
};

const { name, age, address: { city } } = user;

const colors = ["red", "green", "blue"];
const [first, second, ...rest] = colors;

const { nickname = "friend" } = user;

console.log(name, age, city, first, second, rest, nickname);

Part by part:

  • const { name, age } = user; copies the name and age properties into variables with the same names.
  • address: { city } reaches into the nested object and extracts city.
  • const [first, second, ...rest] = colors; assigns the first two array items and puts the remaining items in an array.
  • nickname = "friend" uses a default because user.nickname is missing.

Common mistakes

  • Destructuring from null or undefined. This throws a TypeError. Use a fallback: const { name } = user ?? {};.
  • Using array syntax for objects. const [name] = user; tries to iterate the object, not read a property. Use { name } for objects.
  • Forgetting defaults for optional data. If a property may be missing, write const { email = "" } = user;.
  • Reusing variable names in the same scope. If name already exists, rename it: const { name: userName } = user;.

When to use it

Use destructuring whenUse direct access when
You need several properties or array items at once.You need only one value and the object is already clear.
You want defaults, renaming, or rest values.The key is dynamic, such as obj[key].
You are unpacking function parameters.Unused variables would make the code harder to read.

Practice

Guided exercise: Given const product = { title: "Book", price: 12 };, destructure title and price, and give price a default of 0.

Challenge: From const nums = [1, 2, 3, 4];, extract the first two values, rename the first to primary, and collect the rest.

Hint: Use const { title, price = 0 } = product; and const [primary, second, ...rest] = nums;.

Quick check

What does const { total = 0 } = order; do if order.total is missing?

It assigns 0 to total.

Summary

Destructuring turns repetitive extraction into a compact assignment pattern. Use it when you need multiple values, defaults, or rest data, and prefer direct access when a single dynamic property is clearer.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Destructuring – FAQs

Quick answers about learning Destructuring in JavaScript.

This free note from Coding Now Tech Institute explains Destructuring in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Destructuring, is 100% free with no signup required.
With focused practice, most students grasp Destructuring in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now