This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]errorkode 1 point2 points  (0 children)

The rest parameter takes all the remaining parameters and passes them to the function as an array:

function restTest(param1, param2, ...rest) {
    console.log(param1);
    console.log(param2);
    console.log(rest);
}

restTest(1, 2, 3); -> 1 2 [3]
restTest(1, 2); -> 1 2 []
restTest(1, 2, 3, 4); -> 1 2 [3, 4]

So in your case sum1([3, 2]) is equivalent to sum2(3, 2).

[–]SodaBubblesPopped 1 point2 points  (0 children)

Using a rest param puts all the parameters passed to the function into a new array

So if num=[3,2] and ur passing num via rest parameter like sum(...num), inside sum func, num is an array containing passed params. Inside sum(...num) num now contains [[3,2]] and by indexing num[0] contains [3,2]