3.3 Empty object patterns and Array patterns
Interesting consequence of the algorithm’s rules: We can destructure with empty object patterns and empty Array patterns.
Given an empty object pattern {}
: If the value to be destructured is neither undefined
nor null
, then nothing happens. Otherwise, a TypeError
is thrown.
const {} = 123; // OK, neither undefined nor null
assert.throws(
() => {
const {} = null;
},
/^TypeError: Cannot destructure 'null' as it is null.$/)
Given an empty Array pattern []
: If the value to be destructured is iterable, then nothing happens. Otherwise, a TypeError
is thrown.
const [] = 'abc'; // OK, iterable
assert.throws(
() => {
const [] = 123; // not iterable
},
/^TypeError: 123 is not iterable$/)
In other words: Empty destructuring patterns force values to have certain characteristics, but have no other effects.