Javascript - Three point … - Spread and Rest
Table of Contents
About
The … three points may refer to:
- the spread operator. It 'expands' an array into its elements,
- or the rest parameter. It 'condenses' multiple elements into a single element
Syntax
Spread
The spread syntax 1) allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables (for destructuring assignment) are expected.
Spread syntax can be applied only to iterable objects
- For function calls:
myFunction(...iterableObj);
- For array literals:
[...iterableObj, 4, 5, 6];
Rest
The rest parameter 2) syntax allows to represent an indefinite number of arguments as an array.
- For function definition:
function f(a, b, ...theArgs) {
// ...
}
where:
- …theArgs is an array