An update on rest operator ?

James Browning thejamesernator at
Thu Aug 3 04:44:42 UTC 2017

I still think it's silly that `[, last]` isn't allowed, the point
that it's "iterable destructuring" is actually irrelevant as if there's a
spread it's always coerced to an array e.g.:

function* nums() {
    yield 1
    yield 2
    yield 3
    yield 4

const [first,] = nums()
Array.isArray(rest) // true, it's not an iterator for the rest of the nums,
it's just an Array of [2,3,4]

Given the above, it should really just be the case that `[, last]`
would just be the same as destructuring the reversed array, then
re-reversing the rest part e.g.:

const [, secondLast, last] = someIterable

// Would be equivalent to

const __arr = Array.from(someIterable).reverse()
const [last, secondLast,] = __arr
rest.reverse() // Reverse it back to correct order


Really the only contentious case is if it's in the middle because then you
have to decide which direction to consume from e.g.

const [a,, c] = [1]

// Possible values for a, c
// 1, 1
// 1, undefined
// undefined, 1

Personally I'd be quite happy to *at least* get the `[, last]` case
even if the middle case couldn't be agreed upon, but someone in tc39 would
need to champion it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list