DEV Community 👩‍💻👨‍💻

Discussion on: The tale of three dots in Javascript

Collapse
 
wormss profile image
WORMSS • Edited on

You have a bug in your code.

// old way
const fruitsAndVegetables = fruits.concat(vegetables);
const fruitsAndVegetables = fruits.push('carrot');

The second line will set fruitsAndVegetables to the count of the new length of the array. Not the reference to the array itself

Collapse
 
vuesomedev profile image
Gábor Soós Author

Thanks for noting!

Collapse
 
wormss profile image
WORMSS

Hmmm, now you are mutating fruit array.
Are you sure you were not trying to do something like

const fruitsAndVegetables = fruits.concat(vegetables);
const fruitsAndVegetables = fruits.slice();
fruitsAndVegetables.unshift('carrot');

I don't believe there is a neat little 1 liner to do the equivalent without wrapping 'carrot' in a temp array.

Thread Thread
 
vuesomedev profile image
Gábor Soós Author

I feel the same way...the new syntax is much more compact.