What is the '...' operator in Javascript?

8

I've seen some uses of ... but I'm not sure what it's like. Example:

var a = [1, 2, 3];
var b = [4, 5, ...a];

How do you call this operator and how does it work?

    
asked by anonymous 03.01.2018 / 14:30

1 answer

11

It transforms an object that is a collection of data into a list of data. Its name is spread ( documentation ).

Can be used to transform an array into input arguments of a function, filling another array , or creating an object based on array .

Do not forget that a string does not stop being an array .

In your example b will result in 4, 5, 1, 2, 3 .

It's pretty much how he copied that list of elements and stuck elsewhere waiting for a list. This is not how it works because the data collection need not have been created as a literal, but understand so just to better visualize what is occurring.

this would be more or less the same as:

var a = [1, 2, 3];
var b = [4, 5];
b = b.concat(a);
console.log(b);
    
03.01.2018 / 14:39