Destructuring is a pattern on the left of a declaration, assignment, parameter, or for…of head that pulls values out of the right-hand side.
- Object patterns
{ a, b: renamed, c = 1, ...rest }read properties by key (GetV, so inherited properties and getters count, and primitives are boxed:const { length } = 'abc'works).b: renamedbindsrenamed, notb....restcopies the remaining own enumerable properties into a new plain object. - Array patterns
[x, , z, ...tail]use the iteration protocol, not indexes. They work on any iterable (strings,Set, generators), a hole skips one value, and...tailcollects the rest into a real array. - Defaults
= exprrun only when the value is exactlyundefined.null,0,'', andfalseare kept. The default expression is evaluated lazily, left to right, and may use earlier bindings. - Nesting combines them:
{ user: { address: { city } } }. Onlycityis bound;userandaddressare not.
Where it throws:
- Object pattern on
nullorundefined—RequireObjectCoerciblefails withTypeError. This includes the empty patternconst {} = null. - Array pattern on a non-iterable (
{}, a number,null) —TypeError: … is not iterable. - A nested pattern whose parent value is
undefined:const { a: { b } } = {}readsbfromundefinedand throws. Give the parent a default:{ a: { b } = {} }. - A default that reads a binding declared later in the same pattern hits the temporal dead zone (
ReferenceError).
For assignment (no const/let), a statement starting with { parses as a block, so wrap it: ({ x } = obj);.