1.3 KiB
1.3 KiB
title |
---|
Use the Rest Operator with Function Parameters |
Use the Rest Operator with Function Parameters
Rest parameter explanation
Spread operator compared to rest parameter
Video explaining spread and rest
Example
This code
const product = (function() {
"use strict";
return function product(n1, n2, n3) {
const args = [n1, n2, n3];
return args.reduce((a, b) => a * b, 1);
};
})();
console.log(product(2, 4, 6));//48
Can be written as such
const product = (function() {
"use strict";
return function product(...n) {
return n.reduce((a, b) => a * b, 1);
};
})();
console.log(product(2, 4, 6));//48