What is the difference between spread and rest operators?
// spread: expands
const a = [1, 2];
const b = [...a, 3];
console.log(b);
// rest: collects
function sum(...nums) {
return nums.reduce((t, n) => t + n, 0);
}
console.log(sum(1, 2, 3));
Output:
Tutorials Class - Output Window
[ 1, 2, 3 ]
6
The same ... has different jobs: spread in a call or literal, rest in a function parameter list or destructuring. See ES6 Features.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |