What is SpreadElement in ECMAScript documentation? Is it the same as Spread syntax at MDN?

Solution 1:

The term "spread operator" is kind of an "umbrella term" that refers to various syntactic constructs in ES6 which all look like ...x. MDN does the same.

However, this is misguiding, because ... is not an operator (at least not in the sense the ECMAScript spec uses the term "operator"). It doesn't generate a value that can be used in further computations. I'd rather compare it to other punctuators, such as , or ; (which are also kind of related but have different meaning in different context).

The term "spread operator" could refer to:

  • Spread element, var arr = [a, b, ...c];: The spread element expands the iterable (c) into the new array. It's equivalent to something like [a,b].concat(c).

  • Rest element, [a, b, ...c] = arr;: Inside destructuring, the ... construct has the opposite effect: It collects remaining elements into an array. The example here is equivalent to

    a = arr[0];
    b = arr[1];
    c = arr.slice(2);
    

    (note that this only an approximation, because destructuring works on any iterable value, not just arrays)

  • fun(a, b, ...c): This construct doesn't actually have a name in the spec. But it works very similar as spread elements do: It expands an iterable into the list of arguments.
    It would be equivalent to func.apply(null, [a, b].concat(c)).

    The lack of an official name might be the reason why people started to use "spread operator". I would probably call it "spread argument".

  • Rest parameter: function foo(a, b, ...c): Similar like rest elements, the rest parameter collects the remaining arguments passed to the function and makes them available as array in c. The ES2015 actually spec uses the term BindingRestElement to refer to to this construct.

Related questions:

  • Spread operator vs Rest parameter in ES2015/es6
  • Using spread operator multiple times in javascript?

: If we are very pedantic we would even have to distinguish between a variable declaration (var [a, b, ...c] = d;) and simple assignment ([a, b, ...c] = d;), according to the spec.

Solution 2:

SpreadElement is just a name in the ES6 grammar for spread "operator" together with its argument when in an Array literal:

SpreadElement[Yield]:
    ... AssignmentExpression[In, ?Yield]

So, SpreadElement in [a, b, ...c] is ...c; spread "operator" is .... (scare quotes because it is not a real operator in the same sense that, e.g. - is.)

The name of the grammar rule being used in function calls will be different, as it is a different grammatical context (it's just one kind of ArgumentList).