Destructuring is a concise syntax for unpacking values from arrays (or properties from objects) into distinct variables.
const point = [3, 7];
const [x, y] = point;
console.log(x); // 3
console.log(y); // 7
const [first, , third] = [10, 20, 30];
console.log(first); // 10
console.log(third); // 30
const [head, ...tail] = [1, 2, 3, 4, 5];
console.log(head); // 1
console.log(tail); // [2, 3, 4, 5]
let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b); // 2 1
const [x = 0, y = 0, z = 0] = [10, 20];
console.log(z); // 0 — default applied
const person = { name: "Alice", age: 30, city: "NYC" };
const { name, age } = person;
console.log(name); // "Alice"
console.log(age); // 30
const { name: fullName } = person;
console.log(fullName); // "Alice"
const user = { id: 1, address: { city: "London", zip: "EC1A" } };
const { address: { city } } = user;
console.log(city); // "London"
const coords = [40.7128, -74.0060], use array destructuring to extract latitude and longitude.const product = { name: "Laptop", price: 999, brand: "TechCo" }, destructure name and price.swapPair([a, b]) that takes a two-element array and returns it with elements swapped: swapPair([1, 2]) → [2, 1].#output: "Laptop costs $999 | lat: 40.7128".Array destructuring: const [a, b, c] = myArray;