Learn ES6 (9/31) | Use the Rest Parameter with Function Parameters | freeCodeCamp

Published: 04 May 2021
on channel: Stral Tech
441
3

Learn ES6 (9/31) | Use the Rest Parameter with Function Parameters | freeCodeCamp

In order to help us create more flexible functions, ES6 introduces the rest parameter for function parameters. With the rest parameter, you can create functions that take a variable number of arguments. These arguments are stored in an array that can be accessed later from inside the function.

Check out this code:

function howMany(...args) {
return "You have passed " + args.length + " arguments.";
}
console.log(howMany(0, 1, 2));
console.log(howMany("string", null, [1, 2, 3], { }));
The console would display the strings You have passed 3 arguments. and You have passed 4 arguments..

The rest parameter eliminates the need to check the args array and allows us to apply map(), filter() and reduce() on the parameters array.

Modify the function sum using the rest parameter in such a way that the function sum is able to take any number of arguments and return their sum.


Watch video Learn ES6 (9/31) | Use the Rest Parameter with Function Parameters | freeCodeCamp online, duration hours minute second in high quality that is uploaded to the channel Stral Tech 04 May 2021. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 441 times and liked it 3 visitors.