What is the output of following code?
function sum(a, ...b) {
console.log(b.length);
}
sum(1, 2, 3, 4);
----o----
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
.