Destructuring assignment

suggest change

Introduction

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents