Nearby lessons

11 of 22

JavaScript: Rest Operator

📌 What is Rest Operator?

The Rest Operator ... in JavaScript allows a function to accept an indefinite number of arguments as an array. It's especially useful when working with functions that can take varying numbers of parameters.

1️⃣ Traditional Function (Fixed Parameters)

Without using rest operator, only specific number of parameters are handled. Extra arguments are ignored.

Example02
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

2️⃣ Using arguments object

arguments is an array-like object available inside functions (not in arrow functions).

Example03
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

3️⃣ Rest Operator: Basic Usage

...args collects all remaining arguments into a real array.

Example04
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

4️⃣ Rest Operator with Template Strings

Rest parameters can be used in combination with template strings to make output more readable.

Example05
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

5️⃣ ⚠️ Problem: Rest Operator vs Array Parameter

Be careful when passing an entire array as a single parameter along with the rest operator—it will be treated as one value inside args.

Example06
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3