CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ“€ JavaScript Spread Operator (...) Explained

The spread operator expands arrays and objects into individual elements. Essential for copying, merging, and passing array elements as function arguments.

The spread operator () in JavaScript is used to expand elements of an iterable (like an array or object) into individual elements. -- πŸ“Œ 1. Expanding Arrays Example: Expanding an Array into Function Arguments βœ… The spread operator unpacks the array elements into separate arguments. -- Example: Combining Arrays βœ… Alternative to , creating a new merged array. -- Example: Copying Arrays (Shallow Copy) βœ… Prevents accidental mutation by creating a new array instead of referencing the original. -- πŸ“Œ 2. Using Spread with Objects Example: Merging Objects βœ… Alternative to , creating a new object. -- Example: Overwriting Object Properties βœ… The property in is overwritten by in . -- πŸ“Œ 3. Spread vs Rest () Operator Spread () Expands elements Function calls, arrays, objects β†’ Example: Spread vs Rest in Functions βœ… Spread "spreads out", Rest "gathers in". -- πŸ“Œ 4. Practical Use Cases 1️⃣ Clone and Modify Objects βœ… Useful for immutable state updates (React, Redux, etc.). -- 2️⃣ Remove an Object Property βœ… Removes from without modifying the original object. -- 3️⃣ Convert a String into an Array βœ… Useful for string manipulation. -- πŸš€ Summary Example -- πŸ’‘ Final Thought: The spread operator () is one of JavaScript's most powerful tools for working with arrays, objects, and function arguments in a clean, concise way. -- <!-quiz-start --Q1: What does the spread operator do when used with an array? [ ] Combines arrays by reference [x] Expands array elements into individual elements [ ] Creates a deep copy of nested objects [ ] Removes duplicate elements Q2: What is the result of ? [ ] [x] [ ] [ ] Error: duplicate property Q3: What is the key difference between the spread operator and the rest parameter? [ ] They are the same thing [ ] Spread can only be used with objects [x] Spread expands elements, while rest collects elements into an array [ ] Rest can only be used in function returns <!-quiz-end --
JavaScriptCore Concepts
πŸ›‘ AbortController: Canceling Async Operations in JavaScript
medium
πŸ”’ Closures in JavaScript β€” The Complete Guide
hard
πŸ“¦ Understanding ES6 Modules in JavaScript
medium
⚑ JavaScript Event Loop: Complete Guide to Asynchronous Execution
hard
🧭 Arrow Functions vs Function Declarations in JavaScript
easy
πŸ—‘οΈ Garbage Collection in JavaScript β€” Memory Management & Leak Prevention
hard
πŸ—οΈ Constructor Functions in JavaScript
medium
πŸ‘οΈ MutationObserver: Watching DOM Changes in JavaScript
medium
πŸ” Understanding `of` in JavaScript – `for...of` Loop Deep Dive
hard
πŸ”— Prototype and Prototype Inheritance in JavaScript
medium
πŸ•΅οΈ What Are Proxies in JavaScript? (With Practical Use Cases)
medium
🎯 Scope in JavaScript β€” The Complete Guide
hard
πŸ”„ Script Loading: async vs defer vs Both
hard
πŸ“€ JavaScript Spread Operator (...) Explained
easy
🎯 The JavaScript `this` Keyword: Complete Guide to Context Binding
medium
14 of 15
LibraryJavaScriptCore Concepts14 of 61

πŸ“€ JavaScript Spread Operator (...) Explained

jsgeneral-conceptseasy

The spread operator (...) in JavaScript is used to expand elements of an iterable (like an array or object) into individual elements.


πŸ“Œ 1. Expanding Arrays

Example: Expanding an Array into Function Arguments

function sum(a, b, c) {
  return a + b + c;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers)); // βœ… Equivalent to sum(1, 2, 3) β†’ Output: 6

βœ… The spread operator unpacks the array elements into separate arguments.


Example: Combining Arrays

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

const combined = [...arr1, ...arr2];

console.log(combined); // βœ… [1, 2, 3, 4, 5, 6]

βœ… Alternative to concat() , creating a new merged array.


Example: Copying Arrays (Shallow Copy)

const original = [1, 2, 3];
const copy = [...original];

console.log(copy); // βœ… [1, 2, 3]
console.log(copy === original); // ❌ false (new array, not the same reference)

βœ… Prevents accidental mutation by creating a new array instead of referencing the original.


πŸ“Œ 2. Using Spread with Objects

Example: Merging Objects

const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3, d: 4 };

const merged = { ...obj1, ...obj2 };

console.log(merged); // βœ… { a: 1, b: 2, c: 3, d: 4 }

βœ… Alternative to Object.assign() , creating a new object.


Example: Overwriting Object Properties

const objA = { a: 1, b: 2 };
const objB = { b: 99, c: 3 };

const newObj = { ...objA, ...objB };

console.log(newObj); // βœ… { a: 1, b: 99, c: 3 }

βœ… The b property in objA is overwritten by b in objB.


πŸ“Œ 3. Spread vs Rest (...) Operator

FeatureSpread (...)Rest (...)
PurposeExpands elementsCollects elements
Where it’s usedFunction calls, arrays, objectsFunction parameters
Examplesum(...[1,2,3])β†’sum(1,2,3)function sum(...args) {}

Example: Spread vs Rest in Functions

// Spread: Expands
const numbers = [1, 2, 3];
console.log(Math.max(...numbers)); // βœ… Expands array to arguments

// Rest: Collects
function sum(...args) {
  return args.reduce((acc, val) => acc + val, 0);
}

console.log(sum(1, 2, 3)); // βœ… Gathers all arguments into an array

βœ… Spread "spreads out", Rest "gathers in".


πŸ“Œ 4. Practical Use Cases

1️⃣ Clone and Modify Objects

const user = { name: "Alice", age: 25 };
const updatedUser = { ...user, age: 26 };

console.log(updatedUser); // βœ… { name: "Alice", age: 26 }

βœ… Useful for immutable state updates (React, Redux, etc.).


2️⃣ Remove an Object Property

const person = { name: "Bob", age: 30, city: "NYC" };

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

console.log(rest); // βœ… { name: "Bob", age: 30 }

βœ… Removes city from person without modifying the original object.


3️⃣ Convert a String into an Array

const word = "Hello";
const letters = [...word];

console.log(letters); // βœ… ["H", "e", "l", "l", "o"]

βœ… Useful for string manipulation.


πŸš€ Summary

FeatureExampleUse Case
Function argumentssum(...[1,2,3])Passing array elements as arguments
Array merging[...arr1, ...arr2]Combining arrays
Array copying[...original]Creating a shallow copy
Object merging{ ...obj1, ...obj2 }Merging objects
Object modification{ ...user, age: 30 }Updating properties immutably
String to array[..."Hello"]Splitting a string into characters

πŸ’‘ Final Thought:

The spread operator (...) is one of JavaScript's most powerful tools for working with arrays, objects, and function arguments in a clean, concise way.


Quick Quiz

Test your understanding with 3 quick questions

Q1What does the spread operator do when used with an array?
Q2What is the result of `{ ...{ a: 1, b: 2 }, ...{ b: 3, c: 4 } }`?
Q3What is the key difference between the spread operator and the rest parameter?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna