FrontendDeveloper.in

ECMAScript question detail

Rest parameter

The rest parameter is used to represent an indefinite number of arguments as an array. The important point here is only the function's last parameter can be a "rest parameter". This feature has been introduced to reduce the boilerplate code that was induced by the arguments.

function sum(...args) {
return args.reduce((previous, current) => {
return previous + current;
});
}

console.log(sum(1, 2, 3)); // 6
console.log(sum(1, 2, 3, 4)); // 10
console.log(sum(1, 2, 3, 4, 5)); // 15
Back to all ECMAScript questions
Get LinkedIn Premium at Rs 399