JavaScript Rest parameter

Introduction

Rest parameter was introduced in ES6. It’s useful when you want to handle multiple arguments without explicitly listing them. The rest parameter allows you to collect multiple arguments into a single array, making your code cleaner, more readable, and easier to manage.

What is a Rest Parameter?

The rest parameter allows a function to accept an indefinite number of arguments as an array. It is defined using three dots (…) followed by a parameter name.

📖
Important Notes:
  • The rest parameter must be the last parameter in the function definition.
  • It collects all remaining arguments passed to the function into an array.

Syntax of Rest Parameter


function functionName(...paramName) {
   // code
}

Basic Example 1:


function sum(...numbers) {
   console.log(numbers);
}
sum(1, 2, 3); 

Output:

[1, 2, 3]

Note:  numbers become an array containing all arguments.

Example 2:


function add(...nums) {
   return nums.reduce((sum, num) => sum + num, 0);
}

console.log(add(1, 2, 3, 4, 5)); 

Output:

15

Why Use Rest Parameters?

Rest parameters are useful because they:

  1. Handle unlimited arguments
  2. Replace the arguments object
  3. Provide real array methods
  4. Improve code readability
  5. Support modern JavaScript patterns

Rest Parameter vs Arguments Object

Feature Rest Parameter arguments Object
Type Array Array-like
Methods Yes No
ES Version ES6 ES5
Flexibility High Limited

Using Rest Parameter with Other Parameters

Rest parameter must be the last parameter.

Example:


function display(name, ...marks) {
   console.log(name);
   console.log(marks);
}

display("John", 80, 90, 70);

Output:

John
[ 80, 90, 70 ]

Real-Life Examples: Shopping Cart

In the example below, we will add the total price of the products to the cart.

Example:


function cartTotal(...prices) {
   return prices.reduce((total, price) => total + price, 0);
}
console.log(cartTotal(500, 700, 1000));

Output:

2200

Rest Parameter with Destructuring


const [first, ...rest] = [1, 2, 3, 4];

console.log(first); 
console.log(rest);

Output:

1
[2, 3, 4]

When to Use Rest Parameter

There are many reasons to use Rest Parameter

  • Number of arguments is unknown
  • Working with arrays of data
  • Creating flexible functions
  • Handling dynamic input

rest parameter – Interview Questions

Q 1: What is rest parameter?
Ans: Collects multiple arguments into an array.
Q 2: Syntax of rest parameter?
Ans: ...args.
Q 3: What is the difference between rest and spread?
Ans: Rest collects, spread expands.
Q 4: Can rest be the last parameter?
Ans: Yes, must be last.
Q 5: Use case of rest parameter?
Ans: Handling variable arguments.

rest parameter – Objective Questions (MCQs)

Q1. Rest parameter allows a function to accept ______.






Q2. Rest parameter is written using ______.






Q3. Rest parameters are stored as a(n) ______.






Q4. Where must the rest parameter appear?






Q5. Which keyword was replaced by rest parameters?






Conclusion

The JavaScript rest parameter is a powerful feature that allows functions to handle flexible and dynamic input. It simplifies working with multiple arguments and replaces the older arguments object with a cleaner and more modern approach.

By mastering rest parameters, you can write more efficient, readable, and scalable JavaScript code.