3

I found the following case in javascript. I don't understand '...' operator mean here. I search it on the Google, but I did not get anything about it. Is there any other usage for this operator? Can someone help me out?

var x= [1,2,3];
var y = [4,5,6];

var z = [...x, ...y]; // z will be [1,2,3,4,5,6];

Thanks.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
xiaoke
  • 3,272
  • 2
  • 17
  • 20
  • The `...` operator is called the Spread Operator or Spread syntax. See the [MDN Page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator) about it. – Michael Goodwin Nov 22 '16 at 00:45
  • 9
    [`...` is **not** an operator!](http://stackoverflow.com/a/37152508/218196) – Felix Kling Nov 22 '16 at 00:46
  • @Felix Kling if it's not an operator, what is it? I ask because it's filed under expressions & operators in the MDN docs. – Pineda Nov 22 '16 at 01:04
  • ah wait... it's actually just called _spread syntax_ – Pineda Nov 22 '16 at 01:05
  • @Pineda: It's a punctuator. – Felix Kling Nov 22 '16 at 01:05
  • @Felix Kling: Cool man, thanks. Was just googling this to find a reputable definition. May I ask what yours is (book, article...)? – Pineda Nov 22 '16 at 01:09
  • @Pineda: The ECMAScript specification. `...` is simply part of various constructs, just like `,` is, but it's not a "thing" on its own. – Felix Kling Nov 22 '16 at 01:12
  • @Felix Kling: I just noticed that in "You don't know Javscript: ES6 & Beyond", they refer to the `...` as the spread or rest _operator_. Will this book and the MDN, it's understandable why the lay-folk are confuseth – Pineda Nov 22 '16 at 01:14
  • @Felix Kling: Thanks for the explanation. I guess this is just something to with what people find pleasant in parlance as a name vs the name's technical accuracy. A spread operator sounds 'nicer' than just 'the spread'. Maybe we could lobby for it to be called "triple dot"! – Pineda Nov 22 '16 at 01:20
  • 3
    @Pineda: The spec actually gives names to most of `...`'s use cases. E.g. in this situation, we are talking about a *spread element*, whereas when used in destructuring, it's a *rest element*. This also makes it clearer that the `...` does different things in different contexts. See the link in my first comment as well. Basically I want to make people understand that `...` is not a thing on it's own, its part of the array literal syntax, destructuring syntax, function call syntax, etc. – Felix Kling Nov 22 '16 at 01:26
  • @Felix Kling: Many thanks for the explanation. Kudos in your aim to _spread_ the word 8-) – Pineda Nov 22 '16 at 01:38
  • 1
    @FelixKling We could also simply call it *token*, but "punctuator" is nice :-) – Bergi Nov 22 '16 at 02:12

1 Answers1

1

I have a way of thinking which makes it very easy to understand and remember how '...' works.

var arr = [1,2,3] // this is arr which is the array

on the other hand

...arr            // this is whatever inside arr, which is 1,2,3

So you can also think of it as taking what is inside of an array.


Note that by its own, ...arr is not a valid syntax. You can use it in many ways , two of them coming to my mind are :

1 - Pass what is inside an array to a function

var arr = [ 1,2,3 ]
var myFunc = function(a,b,c) {
  console.log(a,b,c)
}
myFunc(..arr)   // logs 1 2 3

myFunc(1,2,3)   // logs 1 2 3

2 - Take what is inside of an array and use them in another array.

var arr = [ 1,2,3 ]
var foo = [ ...arr, 4,5,6 ] // now foo is [ 1,2,3,4,5,6 ]
FurkanO
  • 7,100
  • 5
  • 25
  • 36
  • Object spread syntax is not available in ES7 either. – Bergi Nov 22 '16 at 01:04
  • Thanks for pointing out. Couldn t find much about it and deleted the part about it. – FurkanO Nov 22 '16 at 01:06
  • 1
    However `...arr` on its own doesn't mean anything. `...arr` has different meaning depending on whether it is used e.g. inside an array literal or inside destructuring. That's why it doesn't make sense to look at `...` on its own. – Felix Kling Nov 22 '16 at 01:28
  • Is my answer misleading ? I can not think of a scenario where this analogy doesn t work @Felix Kling – FurkanO Nov 22 '16 at 01:41
  • 1
    In `[foo, ...bar] = baz;`, `...bar` means something different. `...arr` on its own doesn't mean anything, it's a syntax error. – Felix Kling Nov 22 '16 at 01:51