JS Daily 28 - Rest Parameter in Functions

Last updated on 18 Dec, 2020

What is the output of following code?

function sum(a, ...b) {
sum(1, 2, 3, 4);

The output is 3.

The second argument to the function is a rest parameter. It should always be the last parameter. In the example above, argument 1 is assigned to a. Rest of the arguments are grouped to an array and assigned to b. Therefore, b contains an array [2, 3, 4]. That is why the length is 3.

--- ○ ---
Joby Joseph
Web Architect