... in JS
“…” is an important new feature in ES6, that brings simplicity and elegance to JS.
Briefly the … three dots actually have two different identities in different scenarios. Spread Syntax(Operators) and Rest Syntax(Parameters).
As MDN described it:
Spread syntax
- allows an iterable such as an array expression to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected,
- or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Rest Parameters
- The rest parameter syntax allows us to represent an indefinite number of arguments as an array.
The names are self-explanatory. **Spread operator operates on, and expands array or object. while rest parameters represent the rest parameters in a function definition. **
For examples of these two use cases, go to the MDN site which includes a good number of different ways of using …:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
For a little history of how … changed javascript and life of millions of developers, here is a good article:
- https://dmitripavlutin.com/how-three-dots-changed-javascript/
Written on September 10, 2017
