Saved Bookmarks
| 1. |
What is the rest parameter and spread operator? |
|
Answer» Both rest parameter and spread operator were introduced in the ES6 version of javascript.
return args[1]; } // extractingArgs(8,9,1); // Returns 9 function addAllArgs(...args){ let sumOfArgs = 0; let i = 0; while(i < args.length){ sumOfArgs += args[i]; i++; } return sumOfArgs; } addAllArgs(6, 5, 7, 99); // Returns 117 addAllArgs(1, 3, 4); // Returns 8 **Note- Rest parameter should always be used at the last parameter of a function: // Incorrect way to use rest parameterfunction randomFunc(a,...args,c){ //Do something } // Correct way to use rest parameter function randomFunc2(a,b,...args){ //Do something }
return num1 + num2 + num3 + num4; } let fourNumbers = [5, 6, 7, 8]; addFourNumbers(...fourNumbers); // Spreads [5,6,7,8] as 5,6,7,8 let array1 = [3, 4, 5, 6]; let clonedArray1 = [...array1]; // Spreads the array into 3,4,5,6 console.log(clonedArray1); // Outputs [3,4,5,6] let obj1 = {x:'Hello', y:'Bye'}; let clonedObj1 = {...obj1}; // Spreads and clones obj1 console.log(obj1); let obj2 = {z:'Yes', a:'No'}; let mergedObj = {...obj1, ...obj2}; // Spreads both the objects and merges it console.log(mergedObj); // Outputs {x:'Hello', y:'Bye',z:'Yes',a:'No'};
|
|