Rest Parameters in JavaScript

Javascript 8 min min read Updated: Mar 09, 2026 Intermediate
Rest Parameters in JavaScript
Intermediate Topic 9 of 15

Rest parameters are an ES6 feature in JavaScript that allows a function to accept an unlimited number of arguments. Instead of defining multiple parameters, developers can collect all remaining arguments into a single array using the rest operator (...).

This feature makes functions more flexible and easier to manage when the number of arguments passed to a function is not fixed.

What are Rest Parameters?

Rest parameters allow a function to gather multiple arguments into a single array. The syntax uses three dots (...) followed by the parameter name.

Key Point: Rest parameters collect multiple function arguments into a single array.

Basic Example

In this example, the function collects all arguments into the numbers array.

javascript function showNumbers(...numbers){ console.log(numbers); } showNumbers(1, 2, 3, 4);
Output

[1, 2, 3, 4]

Using Rest Parameters in Calculations

Rest parameters are useful when performing operations on multiple values.

javascript function sum(...numbers){ let total = 0; for(let num of numbers){ total += num; } console.log(total); } sum(5, 10, 15);
Output

30

Key Point: Rest parameters convert multiple arguments into an array automatically.

Using Rest Parameters with Other Parameters

A function can have normal parameters along with rest parameters. However, the rest parameter must always be the last parameter.

javascript function greet(message, ...names){ names.forEach(name => { console.log(message + " " + name); }); } greet("Hello", "Rahul", "Aman", "Priya");
Output

Hello Rahul

Hello Aman

Hello Priya

Difference Between Rest and Spread Operator

  • Rest Operator collects multiple values into an array.
  • Spread Operator expands elements from an array or object.
  • Rest is mainly used in function parameters.
  • Spread is used when passing or copying values.

Advantages of Rest Parameters

  • Allows functions to accept unlimited arguments
  • Improves code readability
  • Replaces the older arguments object
  • Makes JavaScript functions more flexible

Conclusion

Rest parameters provide a modern and convenient way to handle multiple function arguments in JavaScript. By collecting arguments into an array, developers can easily perform operations on multiple values.

This ES6 feature simplifies function design and improves the flexibility of JavaScript programs.

In the next tutorial, you will learn about JavaScript Classes, which provide a cleaner and more structured way to create objects and work with object-oriented programming concepts.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators