I was talking with some friends, today, that included content about a presentation I am thinking about creating on the destructuring feature in ECMAScript 2015. I showed the examples like these.

let [a, , c] = [1, 2, 3, 4, 5];
let {name, heightInCms: height} = {name: 'Curtis', heightInCms: 190.5};

One of my friends is a C++ programmer. C++ programmers think differently than ECMAScript programmers. He asked, can you destructure an entry in an array into the entry of another array?

I had no idea. So, I tried it.

Turns out, you can totally do it.

let source = ['a', 'b', 'c', 'd', 'e'];
let target = [];

[target[0], target[4]] = source;
// node prints: [ 'a', <3 empty items>, 'b' ]

At that moment, I wanted to find out what was what. It makes sense that target[0] is a writable location. How did the ECMAScript standard define the destructuring syntax? Turns out ECMA-262 v6.0 Section 6.2.3 covers The Reference Specification Type which the entries in the destructuring syntax on the left-hand side of the asignment operator must be. The first sentence of the section reads

A Reference is a resolved name or property binding.

So, there we go. A resolved name is a variable and a property binding is properties and, more importantly for this example, index values of an array.

I learned something about ECMAScript, today, and that’s why I like making with the presentations.