Destructuring
Destructuring extracts values from arrays or objects into variables in a single, readable statement.
Destructuring extracts values from arrays or objects into variables in a single, readable statement.
const [x, y] = [10, 20];
const { name, age } = { name: "Alice", age: 30 };
// Swap variables
let a = 1, b = 2;
[a, b] = [b, a]; // a=2, b=1
// Function return
function dims() { return { width: 800, height: 600 }; }
const { width, height } = dims();
Destructuring does not mutate the source — it creates new variable bindings.
More in JavaScript