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.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

2️⃣ Using arguments object

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

Code Example
PREVIEW READY
Loading Editor...
Live Preview

3️⃣ Rest Operator: Basic Usage

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

Code Example
PREVIEW READY
Loading Editor...
Live Preview

4️⃣ Rest Operator with Template Strings

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

Code Example
PREVIEW READY
Loading Editor...
Live Preview

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.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧠 Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!AJS - Spread Operator