Suppose the following:
const arr = [1, 2, 3];
const insertValues = [-1, 0];
const condition = true;
arr.splice(0, 0, condition ? ...insertValues : insertValues);
This throws a syntax error:
Unexpected token '...'
I can get this to work by doing the following:
const arr = [1, 2, 3];
arr.splice(0, 0, ...[-1, 0]);
But this is obviously not what I'd like to do. How can I get the first example to work? I've tried including parentheses where I'd have thought necessary, to no avail.
...(condition ? insertValues : [insertValues])
? The expression needs to be evaluated to a value to pass tosplice
, and if you make it e.g.const thing = condition ? ...insertValues : insertValues;
it should be clear why what you've written makes no sense.fn if (condition) {()}