Destructuring assignment
suggest changeIntroduction
Destructuring binds values from arrays or properties from objects to variables. It was standardized in ECMAScript 2015.
The pattern appears on the left-hand side of a declaration or assignment and describes which values to extract from the expression on the right-hand side.
Syntax
const [x, y] = [1, 2];
const [first, ...rest] = [1, 2, 3, 4];
const [one, , three] = [1, 2, 3];
const [value = "default value"] = [];
const { a, b } = { a: 1, b: 2 };
const { a: renamed } = { a: 1 };
const { nested: { item } } = { nested: { item: "value" } };
Defaults apply only when the extracted value is undefined, not when it is null. Destructuring null or undefined as an object throws a TypeError.
Related examples
- Destructuring objects
- Destructuring function arguments
- Nested destructuring
- Destructuring arrays
- Destructuring inside variables
- Default values while destructuring
- Renaming variables while destructuring
- Using rest parameters to create an arguments array
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents