Master JavaScript Programming From Scratch

Clear, interactive, and structured JavaScript lessons designed for absolute beginners.

Destructuring

Learn how JavaScript destructuring makes it easier to extract values from objects and arrays into separate variables.

What is Destructuring?

Destructuring is a JavaScript syntax that allows us to unpack values from arrays or properties from objects into separate variables. :chatgpt-content-reference{index="1"}

Instead of accessing each value separately, destructuring lets us extract multiple values using a concise syntax. :chatgpt-content-reference{index="2"}

Simple Idea

Destructuring means taking values from an existing object or array and storing those values in separate variables.

JavaScript supports both Object Destructuring and Array Destructuring.

JavaScript object and array destructuring

Destructuring extracts values from objects and arrays into separate variables.

Types of Destructuring

{ }
Object Destructuring

Extracts properties from an object and assigns them to variables.

[ ]
Array Destructuring

Extracts values from an array according to their position.

Object Destructuring Flow
user Object
→
{ name, city } Destructuring Pattern
→
name
city Variables

Object destructuring extracts properties by their property names.

Example 1: Object Destructuring

In object destructuring, the property names are written inside curly braces.

let student = {
    name: "Amit",
    course: "JavaScript",
    city: "Pune"
};

const { name, course, city } = student;

console.log(name);
console.log(course);
console.log(city);
Output: Amit
Output: JavaScript
Output: Pune
Array Destructuring Flow
["Java", "Python", "C#"] Array
→
[first, second, third] Destructuring Pattern
→
Java
Python
C# Variables

Array destructuring assigns values according to their position in the array.

Example 2: Array Destructuring

In array destructuring, the values are assigned according to their index position.

let courses = [
    "Java",
    "Python",
    ".NET"
];

const [first, second, third] = courses;

console.log(first);
console.log(second);
console.log(third);
Output: Java
Output: Python
Output: .NET

Example 3: Renaming Object Properties

Object destructuring also allows us to assign a property to a variable with a different name.

let employee = {
    name: "Sneha",
    department: "Training"
};

const {
    name: employeeName,
    department: team
} = employee;

console.log(employeeName);
console.log(team);
Output: Sneha
Output: Training

Example 4: Default Values

A default value can be provided during destructuring. The default is used when the property is missing or has the value undefined. :chatgpt-content-reference{index="3"}

let course = {
    name: "JavaScript"
};

const {
    name,
    duration = "3 Months"
} = course;

console.log(name);
console.log(duration);
Output: JavaScript
Output: 3 Months
Important Difference

For objects, destructuring matches property names.

For arrays, destructuring matches positions.

Example: const { name } = student; uses the property name, while const [first] = courses; uses the first array position.

Summary

Destructuring provides a concise way to extract values from JavaScript objects and arrays. Object destructuring uses property names, while array destructuring uses element positions. Destructuring can also rename variables and provide default values. :chatgpt-content-reference{index="4"}