Live
Black Hat USADark ReadingBlack Hat AsiaAI BusinessCoreWeave Stock Analysis: Buy or Sell This Nvidia-Backed AI Stock? - The Motley FoolGNews AI NVIDIAIntel Arc B70 Benchmarks/Comparison to Nvidia RTX 4070 SuperReddit r/LocalLLaMAMicrosoft is automatically updating Windows 11 24H2 to 25H2 using machine learning - TweakTownGoogle News: Machine Learning80 Years to an Overnight Success: The Real History of Artificial Intelligence - Futurist SpeakerGoogle News: AIWhat next for the struggling rural mothers in China who helped to build AI?SCMP Tech (Asia AI)Apple reportedly signed a 3rd-party driver, by Tiny Corp, for AMD or Nvidia eGPUs for Apple Silicon Macs; it s meant for AI research, not accelerating graphics (AppleInsider)TechmemeBest Resume Builders in 2026: I Applied to 50 Jobs to Test TheseDEV CommunityTruth Technology and the Architecture of Digital TrustDEV CommunityI Switched From GitKraken to This Indie Git Client and I’m Not Going BackDEV CommunityWhy I Run 22 Docker Services at HomeDEV CommunityHow to Embed ChatGPT in Your Website: 5 Methods Compared [2026 Guide]DEV CommunityThe Spaceballs sequel will be released in April next yearEngadgetBlack Hat USADark ReadingBlack Hat AsiaAI BusinessCoreWeave Stock Analysis: Buy or Sell This Nvidia-Backed AI Stock? - The Motley FoolGNews AI NVIDIAIntel Arc B70 Benchmarks/Comparison to Nvidia RTX 4070 SuperReddit r/LocalLLaMAMicrosoft is automatically updating Windows 11 24H2 to 25H2 using machine learning - TweakTownGoogle News: Machine Learning80 Years to an Overnight Success: The Real History of Artificial Intelligence - Futurist SpeakerGoogle News: AIWhat next for the struggling rural mothers in China who helped to build AI?SCMP Tech (Asia AI)Apple reportedly signed a 3rd-party driver, by Tiny Corp, for AMD or Nvidia eGPUs for Apple Silicon Macs; it s meant for AI research, not accelerating graphics (AppleInsider)TechmemeBest Resume Builders in 2026: I Applied to 50 Jobs to Test TheseDEV CommunityTruth Technology and the Architecture of Digital TrustDEV CommunityI Switched From GitKraken to This Indie Git Client and I’m Not Going BackDEV CommunityWhy I Run 22 Docker Services at HomeDEV CommunityHow to Embed ChatGPT in Your Website: 5 Methods Compared [2026 Guide]DEV CommunityThe Spaceballs sequel will be released in April next yearEngadget
AI NEWS HUBbyEIGENVECTOREigenvector

Spread vs Rest Operators in JavaScript

DEV Communityby Souvik Guha RoyApril 4, 20263 min read0 views
Source Quiz

If you’ve ever seen ... in JavaScript and wondered what it does—you’re not alone. The same syntax is used for two different purposes : Spread operator → expands values Rest operator → collects values Understanding this difference is crucial for writing clean and modern JavaScript. 🧠 What Is the Spread Operator? The spread operator ( ... ) is used to expand elements from arrays or objects. Think: “Open the box and take everything out” 📦 Spread with Arrays ```js id="spread1" const arr1 = [1, 2, 3]; const arr2 = [...arr1, 4, 5]; console.log(arr2); // [1, 2, 3, 4, 5] 👉 It spreads elements individually. --- ### 📊 Visualization ```id="viz1" [1, 2, 3] ↓ ...arr ↓ 1, 2, 3 📦 Copying Arrays ```js id="spread2" const original = [1, 2, 3]; const copy = [...original]; ✔ Creates a shallow copy ✔ Avoi

If you’ve ever seen ... in JavaScript and wondered what it does—you’re not alone.

The same syntax is used for two different purposes:

  • Spread operator → expands values

  • Rest operator → collects values

Understanding this difference is crucial for writing clean and modern JavaScript.

🧠 What Is the Spread Operator?

The spread operator (...) is used to expand elements from arrays or objects.

Think: “Open the box and take everything out”

📦 Spread with Arrays

js
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];


console.log(arr2); 
// [1, 2, 3, 4, 5]


`👉 It spreads elements individually.

---

### 📊 Visualization


```id="viz1"
[1, 2, 3]



...arr



1, 2, 3`


Enter fullscreen mode
 


 Exit fullscreen mode


### 📦 Copying Arrays


```js id="spread2"
const original = [1, 2, 3];
const copy = [...original];


`✔ Creates a shallow copy
✔ Avoids mutation

---

### 📦 Spread with Objects


```js id="spread3"
const user = { name: "Rahul", age: 22 };

const updatedUser = {
 ...user,
 city: "Delhi"
};`


Enter fullscreen mode
 


 Exit fullscreen mode


## 🧠 What Is the Rest Operator?


The rest operator (...) is used to collect multiple values into one.


> Think: “Gather everything into a box”


### 📥 Rest in Function Parameters


```js id="rest1"
function sum(...numbers) {
 return numbers.reduce((total, num) => total + num, 0);
}


console.log(sum(1, 2, 3, 4)); // 10


`👉 All arguments are collected into an array

---

### 📊 Visualization


```id="viz2"
1, 2, 3, 4



...numbers



[1, 2, 3, 4]`


Enter fullscreen mode
 


 Exit fullscreen mode


### 📥 Rest in Destructuring


```js id="rest2"
const [first, ...rest] = [10, 20, 30, 40];


console.log(first); // 10
console.log(rest); // [20, 30, 40]


`---

## ⚖️ Spread vs Rest (Key Differences)

| Feature | Spread Operator | Rest Operator |
| -------------- | ------------------- | --------------- |
| Purpose | Expands values | Collects values |
| Usage position | Right side | Left side |
| Output | Individual elements | Array |

---

## 🛠️ Practical Use Cases

---

### 1. Merging Arrays


```js id="use1"
const a = [1, 2];
const b = [3, 4];

const merged = [...a, ...b];`


Enter fullscreen mode
 


 Exit fullscreen mode


### 2. Function Arguments


```js id="use2"
const nums = [1, 2, 3];


function multiply(a, b, c) {
 return a * b * c;
}


multiply(...nums);


`---

### 3. Removing Properties from Object


```js id="use3"
const user = { name: "Rahul", age: 22, city: "Delhi" };

const { city, ...rest } = user;

console.log(rest);
// { name: "Rahul", age: 22 }`


Enter fullscreen mode
 


 Exit fullscreen mode


### 4. Updating State (Real-world pattern)


```js id="use4"
const state = { count: 0 };


const newState = { ...state, count: state.count + 1 };


`---

## 🧩 Conceptual Understanding

* **Spread → breaks things apart**
* **Rest → puts things together**

---

## 🎯 Common Interview Insight

Interviewers often test:

* Can you merge arrays?
* Can you handle variable arguments?
* Do you understand destructuring with rest?

---

## 🚀 Final Thoughts

The `...` operator is small but powerful.

Mastering **spread vs rest** helps you:

* Write cleaner code
* Handle data efficiently
* Solve interview problems confidently

---

## 🧠 Quick Summary

* Spread → expands values
* Rest → collects values
* Same syntax, different behavior based on context

---`


Enter fullscreen mode
 


 Exit fullscreen mode
Was this article helpful?

Sign in to highlight and annotate this article

AI
Ask AI about this article
Powered by Eigenvector · full article context loaded
Ready

Conversation starters

Ask anything about this article…

Daily AI Digest

Get the top 5 AI stories delivered to your inbox every morning.

More about

updatefeatureinsight

Knowledge Map

Knowledge Map
TopicsEntitiesSource
Spread vs R…updatefeatureinsightDEV Communi…

Connected Articles — Knowledge Graph

This article is connected to other articles through shared AI topics and tags.

Knowledge Graph100 articles · 210 connections
Scroll to zoom · drag to pan · click to open

Discussion

Sign in to join the discussion

No comments yet — be the first to share your thoughts!

More in Releases