Spread vs Rest Operators in JavaScript
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
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
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
Sign in to highlight and annotate this article

Conversation starters
Daily AI Digest
Get the top 5 AI stories delivered to your inbox every morning.
More about
updatefeatureinsight
Why I Run 22 Docker Services at Home
Somewhere in my living room, a 2018 gaming PC is running 22 Docker containers, processing 15,000 emails through a local LLM, and managing the finances of a real business. It was never supposed to do any of this. I run a one-person software consultancy in the Netherlands; web development, 3D printing, and consulting. Last year, I started building an AI system to help me manage it all. Eight specialized agents handling email triage, financial tracking, infrastructure monitoring, and scheduling. Every piece of inference runs locally. No cloud APIs touching my private data. This post covers the hardware, what it actually costs, and what I'd do differently if I started over. The Setup: Three Machines, One Mesh Network The entire system runs on three machines connected via Tailscale mesh VPN: do

I Switched From GitKraken to This Indie Git Client and I’m Not Going Back
I've been using GitKraken for the past three years. It's a solid tool, no doubt. But when they bumped the price to $99/year and started locking basic features behind the paywall, I started looking around. I didn't expect to find anything worth switching to. Then I stumbled on GitSquid. I honestly don't remember how I found it - probably a random thread on Reddit or Hacker News. The website looked clean, the screenshots looked promising, and it had a free tier, so I figured I'd give it a shot. Worst case, I'd uninstall it after 10 minutes like every other "GitKraken alternative" I'd tried before. That was two weeks ago. I've since uninstalled GitKraken. First Impressions The install was fast. No account creation, no sign-in, no "let us send you onboarding emails", just download the DMG, dra

Truth Technology and the Architecture of Digital Trust
The digital economy has entered a credibility crisis. Across industries, borders, and institutions, systems now move information at extraordinary speed, yet too often fail at the more fundamental task of proving what that information actually means. Credentials can be duplicated. Professional claims can be inflated. Identity can be fragmented across platforms. In this environment, the central challenge is no longer access to data, but confidence in its validity. This is not a peripheral issue. It is one of the defining infrastructure problems of the modern technological era. My work sits precisely at this intersection. As a Data Scientist and Full-Stack Developer, I have come to view trust not as a social abstraction, but as a systems problem that must be solved through rigorous engineerin
Knowledge Map
Connected Articles — Knowledge Graph
This article is connected to other articles through shared AI topics and tags.
More in Releases

The Spaceballs sequel will be released in April next year
There's finally a release date for the Spaceballs sequel — but before you get too excited, it's a whole year away. As first reported by Deadline , Amazon MGM Studios announced on Friday night that the upcoming Spaceballs movie will hit theaters on April 23, 2027, right around the 40th anniversary of the first film. Several members of the original cast will be reprising their roles, according to Deadline , including Mel Brooks, Rick Moranis, Bill Pullman, George Wynder and Daphne Zuniga. Spaceballs: The Release Date. April 23, 2027. pic.twitter.com/5Xv0BKmf7C — Amazon MGM Studios (@AmazonMGMStudio) April 4, 2026 Whispers of a potential Spaceballs 2 go back a couple of years, but Brooks officially confirmed in an extremely on-brand announcement video last summer that the movie is actually ha

Template Literals in JavaScript
when you first start javascript building string often involves using the + operator.While this works quickly but it become messy and hard to read as code grows. Before ES6, developers created strings like this: let name = " Alice " ; let age = 25 ; let message = " Hello, my name is " + name + " and I am " + age + " years old. " This approach has several drawbacks: Hard to read: The sentence is broken into multiple parts. Error-prone: Easy to forget spaces or quotes. Messy with complex strings: Adding more variables makes it worse. Difficult for multi-line strings: Requires \n or awkward formatting. Template Literal Syntax Template literals were introduced in ES6 and use backticks (`) instead of quotes. javascript let message = Hello, my name is Alice. ; Embedding Variables in Strings Inste

The Documentation Attack Surface: How npm Libraries Teach Insecure Patterns
Most security audits focus on code. But across five reviews of high-profile npm libraries — totaling 195 million weekly downloads — I found the same pattern: the code is secure, but the README teaches developers to be insecure. One finding resulted in a GitHub Security Advisory (GHSA-8wrj-g34g-4865) filed at the axios maintainer's request. This isn't a bug in any single library. It's a systemic issue in how the npm ecosystem documents security-sensitive operations. The Pattern A library implements a secure default. Then its README shows a simplified example that strips away the security. Developers copy the example. The library's download count becomes a multiplier for the insecure pattern. Case 1: axios — Credential Re-injection After Security Stripping (65M weekly downloads) The code: fo



Discussion
Sign in to join the discussion
No comments yet — be the first to share your thoughts!