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.

  1. const {} = 123; // OK, neither undefined nor null
  2. assert.throws(
  3. () => {
  4. const {} = null;
  5. },
  6. /^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.

  1. const [] = 'abc'; // OK, iterable
  2. assert.throws(
  3. () => {
  4. const [] = 123; // not iterable
  5. },
  6. /^TypeError: 123 is not iterable$/)

In other words: Empty destructuring patterns force values to have certain characteristics, but have no other effects.