Tutorials Modern JavaScript (ES6+) for Beginners

Spread & Rest Operators (...)

Learn Spread & Rest Operators (...) in our free Modern JavaScript (ES6+) for Beginners series. Step-by-step explanations, examples, and interview tips on Toolliyo Academy.

On this page
Spread & Rest Operators (...)
Lesson 6 of 12 · Part 2 — Write Less Code · Modern JavaScript (ES6+) for Beginners
Course: Modern JavaScript (ES6+) for Beginners · Lesson: 6/12 · Read time: ~16 min · Level: Beginner · ES version: ES2015 (ES6) and above

Spread & Rest Operators (...)

The three dots ... do two related jobs: spread expands arrays/objects; rest collects remaining items. Both are everywhere in React (copy state) and Node (merge config).

Spread — copy & merge

const defaults = { theme: 'dark', lang: 'en' };
const userPrefs = { lang: 'hi', fontSize: 16 };

const settings = { ...defaults, ...userPrefs };
// { theme: 'dark', lang: 'hi', fontSize: 16 }

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

Rest — collect remaining

function sum(first, ...rest) {
  return rest.reduce((total, n) => total + n, first);
}
sum(10, 5, 3, 2);  // 20

const [leader, ...team] = ['Sandeep', 'Alex', 'Priya'];
// leader = 'Sandeep', team = ['Alex', 'Priya']
🌍 Real-world example — React-style state update (concept)
const cart = [{ id: 1, qty: 1 }];

function addToCart(cart, newItem) {
  return [...cart, newItem];  // new array — do not mutate old
}

addToCart(cart, { id: 2, qty: 1 });
// cart unchanged; new array returned

React uses this idea: spread creates a new array/object so the UI knows something changed.

⚠️ Common Mistake: Mutating state directly: cart.push(item) then expecting React to re-render. Use spread to return a new array.
💡 Tip: Order matters when merging objects: later spreads overwrite earlier keys.
👨‍🏫 Teaching note: Show shallow copy vs reference: change nested object after spread — nested object is still shared. Deep clone needs other tools.

Continue learning

Previous: Destructuring — Arrays & Objects

Next: Default Parameters & Object Shorthand

Course home: All 12 lessons

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain JavaScript in the context of Modern JavaScript (ES6+) for Beginners.
Short answer: JavaScript runs single-threaded with an event loop. Closures capture lexical scope; promises/async handle I/O without blocking the UI thread. How to structure your answer (60–90 seconds) Define JavaScript i…
Mid Detailed
What are common mistakes teams make with Components when using Modern JavaScript (ES6+) for Beginners?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define Components in plain language…
Senior Detailed
How would you debug a production issue related to State in a Modern JavaScript (ES6+) for Beginners application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define State in plain language for…
Mid Detailed
Compare two approaches to API integration—when would you choose each?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define API integration in plain lan…
Junior Detailed
Describe a real-world scenario where Performance mattered in a Modern JavaScript (ES6+) for Beginners project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define Performance in plain languag…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Modern JavaScript (ES6+) for Beginners
Course syllabus
Part 1 — Modern Basics
Part 2 — Write Less Code
Part 3 — Data & Collections
Part 4 — Async & Modules
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details