Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "..." (triple dot) notation in arrays mean? [duplicate]

People also ask

What is triple dot in array?

When three dots (…) is at the end of function parameters, it's "rest parameters" and gathers the rest of the list of arguments into an array. When three dots (…) occurs in a function call or alike, it's called a "spread operator" and expands an array into a list.

What do the 3 dots in JavaScript mean?

(three dots in JavaScript) is called the Spread Syntax or Spread Operator. This allows an iterable such as an array expression or string to be expanded or an object expression to be expanded wherever placed.

What do 3 dots mean in Typescript?

The three dots are known as the spread operator from Typescript (also from ES7). The spread operator return all elements of an array.

What is the significance of the three dots in this function signature?

operator in it, and it basically means " ... and everything else should go into $strings ". You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $strings array , ready to be used.


The ...(spread operator) works by returning each value from index 0 to index length-1:

As example:

[...'18'] // returns ['1', '8']

which would be the same as:

['18'[0], '18'[1]]

Now, to get an array from 1 to 18, you can do this:

[...Array(19).keys()].slice(1)

Or this with map:

[...Array(18)].map(_=>i++,i=1)

Hope it helps.


The expression [1, 2, 3, ...18] is invalid.

You cannot use ... with a Number. You can only use ... with an iterable object like an Array, String or Object.

It is interesting to note that Tracur - another transpiler - throws an error when fed the same code:

TypeError: Cannot spread non-iterable object.

I am not intimate with the specification but I think this could be a Babel "bug".