Values, Variables, and Memory

Easy

Programs manipulate values stored in memory. Before learning variable syntax, understand what values ARE: numbers, text, true/false, collections. Variables are just labels that point to these values - they are NOT the values themselves.

Interactive Visualization

Variables (Labels)
No variables yet
Memory (Boxes)
42
number
"hello"
string
true
boolean
Step 1/4Memory is like a warehouse with boxes. Each box has an address and can hold a value.
💡 Key Insight:Variables are labels, not containers. They point to where values live in memory.
NumberStringBooleanObjectUndefined

Key Points

  • Programs manipulate values: numbers, text, true/false, collections
  • Values live in memory (think: boxes in a warehouse)
  • Variables are labels that point to memory locations
  • Reading a variable retrieves the value; writing changes what it points to
  • Two variables can point to the same memory location
  • Values have types (number, string, boolean, object, etc.)

Code Examples

Values Are Stored in Memory

// Memory visualization:
// Box A: 42
// Box B: "hello"
// Box C: true

// These are three different values in three memory locations

Think of memory as a warehouse with labeled boxes containing values

Variables Point to Values

let age = 25;
// Variable 'age' points to memory box containing 25

let name = "Alice";
// Variable 'name' points to memory box containing "Alice"

// Reading: What's in the box 'age' points to?
console.log(age);  // 25

// Writing: Make 'age' point to a new box with 26
age = 26;

Variables are names we give to memory locations so we can refer back to them

Copying vs Sharing (Primitives)

let x = 10;
let y = x;  // Copy the value 10 to a new box

x = 20;     // Change what x points to

console.log(x);  // 20
console.log(y);  // 10 (y has its own copy!)

For simple values (numbers, strings, booleans), assignment copies the value to a new box

Types of Values

// Numbers (for math)
42, 3.14, -7

// Strings (for text)
"hello", 'world', `template`

// Booleans (for decisions)
true, false

// Special "nothing" values
null, undefined

// Collections (later topics)
[1, 2, 3], { name: "Alice" }

Every value has a type that determines what operations make sense

Common Mistakes

  • Confusing the variable (label) with the value (what's in the box)
  • Expecting primitive copies to stay "linked"
  • Not understanding that = means "point to" not "equals forever"

Interview Tips

  • This mental model becomes crucial when discussing objects and references
  • Explain "pass by value" vs "pass by reference" using this box analogy
  • Understanding memory helps explain closures and garbage collection