Chapter VIII · Arrays

Reference vs Value

Primitives are stored and copied by value, whereas complex arrays and objects are handled by reference.

01 How It Works

Think of photocopying a text document (value) versus sharing a live shared Google Doc link (reference).

Primitive values duplicate directly, while reference values point to shared memory locations.

example.js
let a = 10;
let b = a; // Copied by value
b = 20;
console.log(a); // 10 (Independent)
  1. 1. Understand primitive value duplication.

  2. 2. Observe reference pointer sharing for arrays/objects.

  3. 3. Use cloning techniques when isolation is required.

02 Practical Example

Here is how you might see this concept applied in real-world code:

practical.js
const arrA = [1, 2];
const arrB = arrA;
arrB.push(3); // Affects arrA too

Key Takeaway

Always account for reference sharing when manipulating arrays and objects.

Related Concepts