Last Updated: February 25, 2016
·
2.631K
· fr0gs

Accessing head/tail of Array ES2015

Before in javascript when I wanted to access the head and tail of an array in a functional way I did:

function head(l) {
   return l[0];
}

function tail(l) {
   return l.slice(1);
}

Now with ES2015 I can just use destructuring and it comes out of the box:

l = [1, 2, 3, 4];
[hd, ...tl] = l;
console.log(hd) // 1
console.log(tl) // [2, 3, 4]