rest spread es6 code example
Example 1: javascript multiply arguments
const mul = (...args) => args.reduce((a, b) => a * b);
// Example
mul(1, 2, 3, 4); // 24
Example 2: spread operator
function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers));
// expected output: 6
// This will add each item in number arrray in sum method.
console.log(sum.apply(null, numbers));
// expected output: 6
Example 3: spread and rest operator javascript
var myName = ["Marina" , "Magdy" , "Shafiq"] ;const [firstName , ...familyName] = myName ;console.log(firstName); // Marina ;console.log(familyName); // [ "Magdy" , "Shafiq"] ;