November 2017
Intermediate to advanced
386 pages
9h 22m
English
The spread operator (see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator) lets you expand an expression in places where you would otherwise require multiple arguments, elements, or variables. For example, you can replace arguments in a function call:
const x = [1, 2, 3];function sum3(a, b, c) { return a + b + c;}const y = sum3(...x); // equivalent to sum3(1,2,3)console.log(y); // 6
You can also create or join arrays:
const f = [1, 2, 3];const g = [4, ...f, 5]; // [4,1,2,3,5]const h = [...f, ...g]; // [1,2,3,4,1,2,3,5]
It works with objects too:
const p = { some: 3, data: 5 };const q = { more: 8, ...p }; // { more:8, some:3, data:5 }
You can also use it to work with functions that expect separate ...