A Simple Guide to JavaScript Spread and Rest Operators

Mitesh KukdejaMitesh Kukdeja
2 min read

Both the spread and rest operators use the same syntax: ... (three dots). But they serve opposite purposes depending on how and where they're used.

Let’s break them down with real-life examples and clean code!


🌟 Spread Operator: Expanding Things

The spread operator is used to spread or unpack elements from arrays or properties from objects.

✅ Use Cases for Spread

  1. Copying Arrays
const fruits = ['apple', 'banana'];
const moreFruits = [...fruits, 'mango'];
console.log(moreFruits); // ['apple', 'banana', 'mango']
  1. Merging Arrays
const a = [1, 2];
const b = [3, 4];
const combined = [...a, ...b];
console.log(combined); // [1, 2, 3, 4]
  1. Copying Objects
const user = { name: 'John' };
const userWithAge = { ...user, age: 25 };
console.log(userWithAge); // { name: 'John', age: 25 }
  1. Merging Objects
const defaultSettings = { darkMode: false, layout: 'grid' };
const userSettings = { darkMode: true };
const settings = { ...defaultSettings, ...userSettings };
console.log(settings); // { darkMode: true, layout: 'grid' }

🌀 Rest Operator: Collecting Things

The rest operator is used to collect multiple values into a single array or object.

✅ Use Cases for Rest

  1. Function Parameters
function sum(...nums) {
  return nums.reduce((total, n) => total + n, 0);
}
console.log(sum(1, 2, 3)); // 6
  1. Destructuring Arrays
const [first, ...rest] = [10, 20, 30, 40];
console.log(first); // 10
console.log(rest);  // [20, 30, 40]
  1. Destructuring Objects
const { id, ...userInfo } = { id: 101, name: 'Alice', role: 'admin' };
console.log(id);       // 101
console.log(userInfo); // { name: 'Alice', role: 'admin' }

🧠 Quick Summary Table

FeatureSpreadRest
Syntax... before array/object... in function params or destructuring
PurposeExpands elementsGathers remaining elements
Used InArrays, objects, function callsFunctions, destructuring
Example[...arr], {...obj}function(...args), const [...x]

🙏 Thank You!

Thank you for reading!

I hope you enjoyed this post. If you did, please share it with your network and stay tuned for more insights on software development. I'd love to connect with you on LinkedIn or have you follow my journey on HashNode for regular updates.

Happy Coding!
Mitesh Kukdeja

0
Subscribe to my newsletter

Read articles from Mitesh Kukdeja directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mitesh Kukdeja
Mitesh Kukdeja

Turning ideas into smooth, scalable mobile experiences — one line of code at a time. Hi, I’m Mitesh Kukdeja — a passionate React Native developer with 2+ years of hands-on experience building cross-platform apps that delight users and deliver results. From health and fitness platforms to job boards and music contest apps, I’ve helped bring a wide range of product visions to life. What sets me apart is my obsession with clean, reusable code and user-centric UI/UX. I specialize in React Native, TypeScript, Redux Toolkit, Firebase, and REST API integration—making sure every app I build is responsive, secure, and ready for scale. I’ve also deployed apps to both the Play Store and App Store, managing the full release cycle. My projects have included integrating real-time features like video conferencing (Agora), personalized push notifications, and advanced security implementations for enterprise clients like Godrej. Whether it’s debugging a performance bottleneck or designing a scalable component architecture, I’m all in. My goal is to keep solving meaningful problems through technology while collaborating with creative minds. I thrive in fast-paced environments where innovation and impact matter. If you’re building something exciting in mobile or looking for a tech partner who values quality and performance — let’s connect!