Destructuring assignment in JavaScript is a concise syntax that allows you to extract values from arrays or properties from objects and assign them to variables. This feature is particularly useful for cleaner and more readable code.
Destructuring arrays allows you to unpack values from an array into separate variables.
const arr = [1, 2, 3];
const [a, b, c] = arr;
console.log(a, b, c); // Output: 1 2 3
const arr = [1, 2, 3, 4];
const [first, , third] = arr;
console.log(first, third); // Output: 1 3
If a value is undefined
, you can assign a default value.
const arr = [1];
const [a, b = 2] = arr;
console.log(a, b); // Output: 1 2
Destructuring objects allows you to unpack properties into variables using their property names.
const person = { name: "Alice", age: 25 };
const { name, age } = person;
console.log(name, age); // Output: Alice 25
You can rename variables while destructuring.
const person = { name: "Alice", age: 25 };
const { name: fullName, age: years } = person;
console.log(fullName, years); // Output: Alice 25
Assign default values for properties that might be undefined
.
const person = { name: "Alice" };
const { name, age = 30 } = person;
console.log(name, age); // Output: Alice 30
You can destructure nested arrays and objects.
const arr = [1, [2, 3]];
const [a, [b, c]] = arr;
console.log(a, b, c); // Output: 1 2 3
const person = { name: "Alice", address: { city: "Wonderland", zip: 12345 } };
const {
address: { city, zip },
} = person;
console.log(city, zip); // Output: Wonderland 12345
You can destructure arrays and objects together.
const data = { id: 1, info: [10, 20, 30] };
const {
id,
info: [first, second],
} = data;
console.log(id, first, second); // Output: 1 10 20
Destructuring is useful for extracting values directly in function parameters.
function sum([a, b]) {
return a + b;
}
console.log(sum([1, 2])); // Output: 3
function greet({ name, age }) {
return `Hello, ${name}. You are ${age} years old.`;
}
console.log(greet({ name: "Alice", age: 25 })); // Output: Hello, Alice. You are 25 years old.
The rest pattern collects the remaining elements or properties into a separate variable.
const arr = [1, 2, 3, 4];
const [a, b, ...rest] = arr;
console.log(a, b, rest); // Output: 1 2 [3, 4]
const person = { name: "Alice", age: 25, country: "Wonderland" };
const { name, ...others } = person;
console.log(name, others); // Output: Alice { age: 25, country: 'Wonderland' }
- Extracting API Response Data: Simplify working with nested response data.
- Swapping Variables:
let a = 1,
b = 2;
[a, b] = [b, a];
console.log(a, b); // Output: 2 1
- Assigning Variables from Arrays:
const rgb = [255, 100, 50];
const [red, green, blue] = rgb;
console.log(`Red: ${red}, Green: ${green}, Blue: ${blue}`);
- Providing Default Parameters in Functions:
function createUser({ name = "Guest", isAdmin = false } = {}) {
return { name, isAdmin };
}
console.log(createUser()); // Output: { name: 'Guest', isAdmin: false }
Destructuring assignment is a powerful feature in JavaScript that simplifies variable assignment and improves code readability. Whether you're working with simple arrays or deeply nested objects, destructuring makes it easier to extract the data you need.