Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / JavaScript Tutorial / Chapter 3: Data Structures & Modern ES6+
JAVASCRIPT ⏱ 10 min read

Modern ES6+ Features (Destructuring, Spread, Rest)

Modern JavaScript introduced expressive syntax features that dramatically reduce boilerplate and make code clean and readable.

What is it?

ES6+ syntax includes Object & Array Destructuring, Spread Operator (`...`), Rest Parameters (`...`), Shortened Object properties, and Default Parameters.

Why is it used?

Unpacks properties in one line, clones objects immutably, merges configs, and handles flexible function arguments.

Detailed Explanation

1. Destructuring: Unpacking values from arrays or properties from objects into distinct variables: • `const { name, email } = user;` • `const [first, second] = list;` 2. Spread Operator (`...`): Expands an iterable into individual elements: • Shallow clone: `const clone = { ...original };` • Overriding props: `const updated = { ...original, role: 'admin' };` 3. Rest Parameters (`...`): Gathers indefinite remaining arguments into an array: • `function sum(...numbers) { return numbers.reduce((a, b) => a + b, 0); }`

Syntax Structure

Syntax Blueprint
const { title, price } = course;
const updatedCourse = { ...course, discountedPrice: 2000 };
function logAll(...args) { ... }

Code Example

JAVASCRIPT Code
// 1. Object Destructuring with Default Values and Renaming
const studentRecord = {
  id: 101,
  fullName: "Karan Deshmukh",
  contact: { phone: "9876543210", city: "Nanded" },
  enrolled: true
};

const { fullName, contact: { city }, score = 85 } = studentRecord;
console.log(`${fullName} lives in ${city}, score: ${score}`);

// 2. Immutable Object Update via Spread
const originalConfig = { theme: "light", notifications: true, fontSize: 16 };
const userPreferences = { ...originalConfig, theme: "dark", fontSize: 18 };
console.log("User Preferences:", userPreferences);

// 3. Rest Parameters Function
const calculateTotal = (...prices) => prices.reduce((acc, p) => acc + p, 0);
console.log("Total of 3 items:", calculateTotal(500, 2500, 1500));

Explanation of the Code

• Nested destructuring extracts `city` from inside `contact`. • Spread `{ ...originalConfig, theme: "dark" }` creates a new object with updated values without mutating the original. • `...prices` captures any number of numeric arguments into a real array.

Expected Output

Karan Deshmukh lives in Nanded, score: 85 User Preferences: { theme: 'dark', notifications: true, fontSize: 18 } Total of 3 items: 4500

Real-World Application

Used in modern state management (Redux, React setState) where mutating state directly is prohibited.

Common Mistakes to Avoid

• Confusing Spread (unpacks items) with Rest (gathers items). • Assuming Spread creates a deep clone (it only performs a shallow copy of top-level properties).

Quick Practice Challenge

Given `const user = { name: 'Alex', age: 25, role: 'student' };`, destructure `name` and `role` into variables.

Key Takeaways

• Destructuring extracts properties into distinct variables cleanly. • Spread (`...`) allows safe immutable object and array copies. • Rest (`...args`) gathers multiple arguments into an array.