Rest Parameter in Javascript




Like Spread Spread Operator the Rest Parameter shares the same syntax. The three dots ... .The difference is both of them behave different. The spread operator will expand the contents of an iterable. In contrast, the rest parameter collects all the remaining elements into an array.

Example 1

If we run the program, it will produce the result as 14. In the addValues() function we can pass any number of parameters and get the sum of those supplied parameters. The addValues() function gets all the parameters using rest paramters in 'params' parameter. Now we can call addValues() function with any number of integer parameters to return the result of sum of parameters.

Example 2

If we run the program, it will produce the result as below

1
[2,3]

Example 3

If we run the program, it will produce the result as below.

22
{firstName: "Readers", lastName: "Buddy", age: 2}

Most Read