MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 10
Welcome to my daily blog once again as I take you through my JavaScript coding challenge. I grasp the knowledge of rest and spread in JavaScript ES6 today.
REST
Explanation: Rest is used in JavaScript for packing values into arrays. It is useful in turning function arguments into an array if the number of possible arguments is not certain.
Example
function sum(...numbers) {
let result = 0;
for (let i =0; i < numbers.length; i++){
result += numbers[i];
}
return result;
};
console.log(sum(2,4)); // prints 6
console.log(sum(4,5)); // prints 9
SPREAD
Explanation: Spread is used to unpack values from an array. It is useful in a function if only certain amount of arguments are needed from an array.
Example
function summation(x,y,z) {
return x + y + z;
};
console.log(summation(...[3,4,5,6,7])); //prints 12