Destructuring

An ES6 feature that is syntactic sugar for declaring variables from values within objects and arrays.


Object Destructuring

const { x, y, z } = { x: 1, y: 2 };
x;
1
y;
2
z;
undefined
const { foo, ...rest } = {
  foo: 'hello',
  bar: 'world'
};
foo;
'hello'
rest;
{ bar: 'world' }

Array Destructuring

const [a, b, c] = ['a', 'b'];
a;
'a'
b;
'b'
c;
undefined
const [first, ...rest] = [1, 2, 3];
first;
1
rest;
[2, 3]

External Resources