Base CS from zero
Variables and state: multiple-choice review
Six questions that cut across the whole unit. Each one is a decision about what is actually stored in a cell — a value or a reference — and what changes when you assign or mutate. Get the memory model right and aliasing bugs stop being surprises.
Confirm you can connect the four ideas the unit built up: a variable is a name bound to a cell, assignment overwrites that cell, mutation changes state in place, and primitives copy while objects share by reference.
After 'let score = 0', the program runs 'score = 42'. What changed in memory?
Why is reading '=' as 'equals' a trap, and what should you read it as instead?
A bug report says 'I changed obj2 and obj1 mutated on its own.' The code was 'let obj2 = obj1'. What actually happened?
'const user = { name: "A" }; user.name = "B";' runs without error. Yet 'const x = 1; x = 2;' throws. Why?
'let a = [1,2]; let b = a; b.push(3);' — then code elsewhere reads a.length expecting 2. Why does it see 3?
What is the difference between a program's 'state' and the 'mutations' that produced it?
The through-line: a variable is a name bound to a cell, and the name and the value are different things. Assignment is a destructive store — ‘gets’, not ‘equals’. Mutation changes a cell in place, and state is the snapshot of all current cell contents. The decisive split is what sits in the cell: a primitive value (copied on assignment, so changes do not propagate) or a reference to a heap object (copied as an address, creating aliases where one mutation is visible through every name). const seals which reference a cell holds, never the contents behind it.