Why can't spread operator be used multiple times?
let arr = [[[1, 2, 3]]];
console.log(arr); // Array [ Array[1] ]
console.log(...arr); // Array [ Array[3] ]
console.log(...(...arr));
// SyntaxError: expected '=>' after argument list, got ')'
I would expect:
console.log(...(...arr)); // Array [ 1, 2, 3 ]
Because ...arr isn't like a function that returns a value in normal scenarios (you can test this by just typing ...[[1,2,3]] in console, if ... operated like a normal function we would expect a return of [1 2 3]. For that reason you can't chain spreads. From MDN:
Ergo, spreads need to happen within array literals, object literals (if using obj spread, which is ES7) or within function calls So you could do console.log(...[].concat(...arr))
...
is not an operator.(...arr)
is not valid JavaScript....
is only allowed inside array literals and in arguments lists, but those are special forms of the syntax (notice the...
in the production rules below).ArrayLiteral
Arguments