Base CS from zero
Mutation and state
You know how to declare a variable and how assignment overwrites a cell. Now step back and ask: what does a running program actually look like inside the machine at any given moment?
At any instant, each variable holds some value. The score is 10. The lives counter is 3. The game-over flag is false. These values together describe what the program currently is — not what it was a second ago, and not what it will be next. Right now, in this moment: that collection of cell contents.
That collection is called the program’s state. Changing any one of those values — overwriting a cell — is called mutation. These two words appear everywhere in computer science and software engineering. Understanding exactly what they mean, in terms of the memory model you have been building, makes them precise instead of vague.
After this lesson you can define program state as a snapshot of variable values, explain mutation as an in-place change to a cell, and reason about why state and mutation are central concepts in software design.
Program state: the snapshot of all current values. At any point in time, every live variable has a current value — the bit pattern sitting in its bound memory cell at that instant. The program state (or just “state”) is the complete collection of those current values.
Think of state as a photograph of all the cells at one moment in time. If you took this
photograph at time T, you would see: score = 10, lives = 3, isGameOver = false.
One millisecond later, after an assignment, the photograph looks different: score = 15.
State is always about now — the current snapshot.
The word “state” comes from the same root as “status”: the condition of something at a given instant. A program’s state is its current condition, expressed as the set of values its variables hold.
Mutation: changing a value in place. When an assignment overwrites a cell — when the bit pattern at some address changes — that change is called a mutation. The cell exists at the same address before and after; only its contents changed. The mutation happened in place.
“In place” is the critical phrase. Mutation does not create a new cell and copy data. It modifies the existing cell directly. The old value is gone, overwritten in the same physical location. The cell’s address is unchanged; the cell’s contents are different.
Every assignment you have written so far (score = score + 5) is a mutation: it changes
the value inside an already-existing cell.
Why this works
Why do programmers use the word “mutation” rather than simply “change”? The word carries a specific connotation from biology: a mutation alters something from the inside, in place, rather than replacing it with something entirely new. In programming, the distinction matters because mutation has side effects — other parts of the program that can write to the same cell see the changed value. This is contrasted with immutable data, where instead of modifying a value, you produce a new value and leave the original untouched. The biological metaphor is deliberately chosen.
State as the sum of all mutations so far. A program does not hold just one value — it holds many variables simultaneously. State is the aggregate of all their current values. Each mutation changes one piece of that aggregate.
Think of a game loop. Every frame:
- The player moves →
playerXmutates. - An enemy is destroyed →
enemyCountmutates. - Score updates →
scoremutates. - Time passes →
elapsedMsmutates.
After each frame, the state is different from what it was before. The state is the running tally of everything that has happened so far, encoded in the current values of all variables.
This is why the word “state” matters: it captures the idea that the program’s condition at any moment is the cumulative result of all prior mutations.
Stateful programs vs stateless computations. Not every computation involves mutation. A function that takes two numbers and returns their sum does not need to change any cell — it just computes and returns. This is a stateless computation: it depends only on its inputs, not on any accumulated history.
A stateful program, by contrast, stores values across multiple steps and modifies them over time. Games, user interfaces, servers handling requests — all of these are stateful: they accumulate history in variables and their behaviour at any moment depends on what happened before.
Distinguishing stateful from stateless design is one of the foundational skills in software engineering. Stateless code is easier to test (give it inputs, check the output, no memory of previous calls). Stateful code is more powerful but harder to reason about.
Common mistake
“State” and “mutation” are sometimes used loosely to mean simply “variables exist”. But
the precise meaning is narrower: state is the current values of variables at a
specific point in time; mutation is the act of changing one of those values in
place. You can have state without mutation — a snapshot of values that never change
(const declarations only). Mutation without state is harder to define, but the point
is: the two concepts are related but distinct. Keeping them precise helps you reason
about bugs.
Why state causes bugs. The power of mutable state — a variable that tracks
accumulated history — is also its danger. If two parts of a program can both mutate the
same variable, they can interfere with each other. One part sets score = 0 to reset
the game; another part was mid-calculation expecting score to be non-zero. The
mutation by the first part left the second part with a wrong assumption.
This class of bug — one mutation invalidating another part of the program’s expectations — is the reason so much of software engineering concerns controlling mutation: limiting which parts of a program can change which variables, and when.
Tracing state across multiple mutations.
let score = 0;
let lives = 3;
let level = 1;State snapshot at line 3: { score: 0, lives: 3, level: 1 }
score = score + 10; // player scoredState snapshot after line 4: { score: 10, lives: 3, level: 1 }
Mutation: score changed in place. lives and level untouched.
lives = lives - 1; // player diedState snapshot after line 5: { score: 10, lives: 2, level: 1 }
Mutation: lives changed in place. score and level untouched.
level = level + 1; // next level
score = 0; // reset scoreState snapshot after line 7: { score: 0, lives: 2, level: 2 }
Two mutations in sequence: level and score each changed.
At every step, “state” is the complete current snapshot. “Mutation” is each individual in-place change.
let x = 5; let y = 10; x = x + y; How many cell writes (mutations) occurred across all three lines? Type the number.
let a = 1; let b = 2; a = b; b = 5; What is the value of a?
A program has variables x=3, y=7, z=1. After 'x = y', what is the state of x?
let counter = 0; counter = counter + 1; counter = counter + 1; counter = counter + 1; What is counter?
True or false (1=true, 0=false): mutation changes a cell's value in place without allocating a new memory cell.
What is program state?
State is the complete snapshot of current values held by all live variables at one instant. Mutation is an in-place change: an assignment that overwrites a cell at the same address, destroying the previous value. State is the cumulative result of all prior mutations. Every time a variable is assigned a new value, the state changes — exactly one cell’s contents are different, and the rest of the snapshot is unaffected. Stateful programs store and modify values over time; stateless computations depend only on their inputs. Understanding the boundary between the two is fundamental to reasoning about how programs behave and where bugs come from.