JavaScript [a, b] = Destructuring assignment Operator
Destructuring assignment unpacks values from arrays or properties from objects into separate variables in a single statement.
Description
Destructuring lets you unpack values from arrays, or properties from objects, into distinct variables using a pattern that mirrors the data's shape. Array patterns use position, const [first, second] = list; object patterns use property names, const { name, age } = user.
Patterns can rename ({ name: userName }), give defaults ({ size = 'm' }), skip items ([, second]), collect the rest ([head, ...tail]) and nest. Destructuring also works in function parameters and for...of loops, and swapping two variables is as simple as [a, b] = [b, a].
Destructuring a value that is null or undefined throws a TypeError, so give a default for optional objects, as in const { a } = options ?? {}, or a parameter default, function f({ a } = {}). When assigning to existing variables with an object pattern, wrap the whole statement in parentheses, ({ a, b } = obj), so the braces aren't read as a block.
Properties
- Operator
[a, b] =- Category
- Assignment
- Precedence
- 2 (1 is lowest, 18 is highest)
- Associativity
- right-to-left
Syntax
const [a, b] = array;
const { name, age } = object;
const { name: alias = defaultValue, ...rest } = object;Example
Browser Support
The following information will show you the current browser support for the [a, b] = operator. Hover over a browser icon to see the version that first introduced support for this operator.
Desktop





Tablets & Mobile





