JavaScript Destructuring

Beginner
6 min read
Updated: Nov 2026
3 examples

Definition and Usage

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

JavaScript Syntax

JavaScript
const [first, second] = [10, 20];
const { name, age } = { name: "Ada", age: 36 };

console.log(first, second);
console.log(name, age);
Try it Editor

How It Works

  1. Array pattern. Positions on the left match items on the right.
  2. Object pattern. Names on the left match property keys.
  3. One statement. You declare several variables without repeating the source.

Examples

Example 1 — Swap and Skip

Swap two variables and skip an item with a hole.

JavaScript
let a = 1;
let b = 2;
[a, b] = [b, a];
const [head, , tail] = ["x", "y", "z"];
console.log(a, b);
console.log(head, tail);
Try it Editor

How It Works

  1. Swap. Right-hand array builds new values, then assigns back.
  2. Skip. An empty slot ignores "y".

Example 2 — Rename and Default

Pull fields under new names with fallbacks.

JavaScript
const user = { id: 7, name: "Ada" };
const { name: label, role = "dev" } = user;
console.log(label, role);
Try it Editor

How It Works

  1. Rename. name: label copies name into label.
  2. Default. Missing role becomes "dev".

Example 3 — Destructure Parameters

Unpack an options object in the parameter list.

JavaScript
function fullName({ first, last }) {
  return first + " " + last;
}
console.log(fullName({ first: "Ada", last: "Lovelace" }));
Try it Editor

How It Works

  1. Pattern in params. The function receives an object and names its fields.
  2. Clean call site. Pass one options object.

Frequently Asked Questions

Syntax that copies array items or object properties into variables in one go.

No. It reads values; the original array or object stays intact unless you assign back into it.

Yes. You can destructure objects inside arrays and the other way around.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful