Module 02 // Core JavaScript

Data Types & Values

Module Objective

Primitives, objects, mutability, value vs reference, typeof, instanceof

Mental Model Realtime Simulation

INTERACTIVE_CANVAS
Editor_Pane
Loading...
Console_Output
Waiting for output...

Practical Code Examples

// Example 1
// 1. Primitive Immutability
let name = "Subhajit";
name[0] = "A"; // ❌ Silently fails (or error in strict mode)
console.log(name); // "Subhajit" (Unchanged)

name = "Alex"; // ✅ Reassigning the variable is allowed
console.log(name); // "Alex"
💡 Primitives are **immutable**. You cannot change a character inside a string; you can only replace the entire string with a new one.
// Example 2
// 2. Value vs Reference
// Primitives: Copied by Value
let x = 10;
let y = x;
y = 20;
console.log(x); // 10 (Independent)

// Objects: Copied by Reference
let user1 = { name: "Alice" };
let user2 = user1; // Both point to the SAME object in memory
user2.name = "Bob";
console.log(user1.name); // "Bob" (Affected!)
💡 Primitives store the actual value. Objects store a **memory address** (reference). Modifying one reference affects all others pointing to that object.
// Example 3
// 3. Null vs Undefined
let a;
console.log(a); // undefined (System default for "not assigned")

let b = null;
console.log(b); // null (Developer choice for "intentionally empty")
💡 `undefined` means a variable has been declared but not yet assigned a value. `null` is an assignment value representing 'no value'.

Engine & Memory Architecture

Storage Strategy

1. The Stack (Primitives):

  • Primitives are stored in the Stack.
  • They have a fixed size and are extremely fast to access.
  • When you pass a primitive, JS copies the actual value.

2. The Heap (Objects):

  • Objects, Arrays, and Functions are stored in the Heap.
  • The Heap is large, unstructured memory for data whose size might change.
  • The Stack only stores a pointer (reference) to the Heap address.

Hardware Context

  • CPU: Handles the Stack operations directly via registers (super fast).
  • RAM: Both Stack and Heap live in RAM, but the Heap requires more "walking" through memory to find data.