› Module 4: Objects & Arrays › 4.5 Spread & Rest Operators
M O D U L E

Module 4: Objects & Arrays

JavaScript's core data structures

4.1 Objects

An object is a collection of key-value pairs used to represent structured data and real-world entities. Keys (also called properties) are strings or symbols, and values can be any type — including primitives, nested objects, or functions (called methods when attached to an object).

Figure 4.1: Object Memory Model — Stack Reference to Heap Structure Call Stack (Variables) const person = 0x4F2A (Heap Pointer) Points to Heap Memory (0x4F2A) name: "Sam", age: 25, greet: [Function]

Figure 4.1: The variable on the call stack holds a pointer reference to the mutable object stored inside the memory heap.

const person = {
  name: "Sam",
  age: 25,
  greet() {
    console.log(`Hi, my name is ${this.name}`);
  }
};

person.greet(); // "Hi, my name is Sam"
console.log(person.age); // 25

4.2 Arrays

An array is an ordered, zero-indexed list of values. Unlike low-level languages with fixed contiguous memory buffers, JavaScript arrays are dynamically resizable objects that can hold heterogeneous data types.

Figure 4.2: Array Methods — Pure Non-Mutating (slice) vs. In-Place Mutating (splice) arr.slice(start, end) • NON-MUTATING Returns a shallow copy of sub-array [1, 2, 3].slice(0, 2) → [1, 2] (Original is intact!) arr.splice(start, count, ...items) • MUTATING Removes, replaces, or inserts elements in-place Directly changes original array in memory!

Figure 4.2: Difference between non-mutating slice() (pure) and mutating splice() (in-place modification).

4.3 Array Methods (map / filter / reduce)

These three higher-order methods form the backbone of functional array data transformations in modern web applications, eliminating error-prone manual loops.

Figure 4.3: Functional Array Transformations (map, filter, reduce) [1, 2, 3, 4] map() 1-to-1 Transformation nums.map(x => x * 2) → [2, 4, 6, 8] filter() Subset Matching Test nums.filter(x => x > 2) → [3, 4] reduce() Accumulate Single Value nums.reduce((a,b)=>a+b, 0) → 10

Figure 4.3: Transforming array [1, 2, 3, 4] into a doubled array using map, a filtered subset using filter, and an accumulated scalar sum via reduce.

4.4 Destructuring

Destructuring is an expressive ES6 syntax pattern allowing you to extract individual values from arrays or object properties into distinct variables in a single declarative line.

Figure 4.4: Destructuring Syntax Unpacking Models Object Destructuring (Matches Keys) const { name, age } = person; Extracts properties by name regardless of order Array Destructuring (Matches Position) const [first, second] = nums; Extracts elements by numeric index position

Figure 4.4: Object destructuring matches by property key name, while array destructuring unpacks items strictly by index position.

4.5 Spread & Rest Operators

Both spread and rest utilize the three-dot token (...), but perform inverse operations determined strictly by context: spread expands an iterable into elements, while rest gathers remaining items into an array.

Figure 4.5: Spread (Unpack) vs. Rest (Gather) Execution Comparison Spread (...) • Unpacks Iterable const arr2 = [...arr1, 3, 4]; Cloning and merging collections Rest (...) • Gathers Elements function sum(...numbers) {} Bundles unbounded arguments into an Array

Figure 4.5: Spread unpacks arrays and objects, while rest gathers arguments into real JavaScript Arrays.

MODULE COMPREHENSIVE QUIZ

Module 4 Final Quiz

Test your understanding across all 5 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 4 Final Quiz →
Yukti Mascot