awesome-everything RU
↑ Back to the climb

Base CS from zero

Assignment

Crux Assignment is a store operation: the runtime evaluates the right-hand side, then writes the result into the cell bound to the variable on the left. The previous value is gone. The name stays; only the cell''''s contents change.
◷ 22 min

You declared a variable. You bound a name to a cell and wrote an initial value into it. Now what? Programs need to change values over time — counters go up, totals accumulate, status flags flip. The mechanism that changes the value in a cell is called assignment.

Assignment looks deceptively simple: x = 5. But there is a precise machine operation hiding behind that line. The runtime evaluates the right side, takes the result, and writes it into the cell that the name on the left is bound to. The cell’s old value is gone. Everything else stays the same — the binding, the name, the address — only the cell’s contents change.

Understanding that = in programming is not the mathematical equality sign — it is a store command — is one of the most important early shifts in learning to program.

Goal

After this lesson you can explain what assignment does at the memory-cell level, trace through a sequence of assignments to see what value a variable holds at each step, and distinguish let (rebindable) from const (single-assignment).

The idea

Assignment as a store operation. When the runtime executes x = expr, it performs two steps in order:

  1. Evaluate the right-hand side expression expr. This produces a value — a bit pattern. The expression can be a literal (5), a variable name (y), arithmetic (x + 1), or anything else that yields a value.
  2. Write that value into the memory cell whose address is bound to the name x. The previous contents of the cell are overwritten and lost.

The name on the left side of = is not evaluated — it is not read. It is resolved to an address, and that address is used as the destination for the write. This is why = in code is pronounced “gets” or “is assigned”, not “equals”: x = 5 means “x gets 5”, not “x is equal to 5”.

First assignment vs re-assignment. There are two distinct moments in a variable’s life:

  • Declaration with initial valuelet x = 0; — reserves the cell, establishes the binding, and writes the initial value 0 into the cell. This is the first assignment.
  • Re-assignmentx = 42; — looks up the existing binding, finds the cell’s address, and writes the new value 42, overwriting 0. The binding is unchanged.

const prevents re-assignment. TypeScript and JavaScript also have const:

const y = 10;
y = 20; // TypeError: Assignment to constant variable.

const means the binding is sealed after the first assignment — no further writes to that binding are allowed. The cell is written exactly once. This is not about the value being frozen in some deep sense (objects declared const can still have their properties mutated — that is covered in lesson 04), but about the name→cell binding accepting exactly one store.

Evaluation order matters. Consider x = x + 1. The right side is evaluated first: the runtime reads the current value of x, adds 1, then writes the result back to the same cell. Steps: (1) read x → get current value, (2) add 1, (3) write result to x. The cell ends up holding one more than it held before. The old value is gone.

The code
1 // Declaration — reserves a cell and writes the initial value.
2 let score = 0; // cell at (say) addr 1042 → contains 0
3
4 // Re-assignment — overwrites the cell's contents.
5 score = 10; // addr 1042 → now contains 10
6 score = score + 5; // addr 1042 → read 10, add 5, write 15
7 score = score * 2; // addr 1042 → read 15, multiply 2, write 30
8
9 // The name 'score' never changed; only the cell's value changed.
10 console.log(score); // 30
11
12 // const seals the binding after the first write.
13 const MAX = 100;
14 // MAX = 200; // would throw TypeError — binding is sealed
  • L2 let: declares a new variable, reserves a cell, writes initial value 0
  • L5 Re-assignment: overwrites the cell. Old value 0 is gone.
  • L6 RHS evaluated first: read score (10), add 5, result 15. Then write 15 to the cell.
  • L7 RHS evaluated first: read score (15), multiply 2, result 30. Then write 30.
  • L10 Reading 'score' returns whatever is in the cell right now: 30.
  • L13 const: first (and only) write is the declaration. Binding is now sealed.
  • L14 A second write to a const binding throws at runtime.
Step-by-step trace

Trace every step as score changes from declaration through three re-assignments.

1 let score = 0;
2 score = 10;
3 score = score + 5;
4 score = score * 2;
5 console.log(score);

Common mistake

x = x + 1 looks circular — “x is x plus one?” — but it is not an equation. It is a two-step command: first compute x + 1 (reading the old value), then store the result back in x. The right side is fully evaluated before anything is written. After the line runs, x holds one more than it did before. Reading and writing the same cell in one statement is perfectly valid and extremely common.

Edge cases

What happens if you swap two variables? Naively:

let a = 1;
let b = 2;
a = b;   // a → 2, b still 2
b = a;   // b → 2 (! we wanted 1)

The problem: once a = b runs, the original value of a (1) is gone. The fix: save the original value first:

let temp = a;  // temp → 1, a → 1, b → 2
a = b;         // a → 2
b = temp;      // b → 1

Now both cells end up with the swapped values. This classic three-step swap is a direct consequence of assignment being a destructive overwrite.

Practice 0 / 5

let x = 3; x = 7; What value does x hold after the second line?

let n = 10; n = n + 4; What value does n hold?

let a = 5; let b = a; a = 99; What value does b hold?

let c = 2; c = c * c; c = c * c; What value does c hold?

let x = 1; x = x + x; x = x + x; What value does x hold?

Check yourself
Quiz

What exactly happens in memory when 'x = x + 1' executes?

Recap

Assignment is a store operation with two steps: evaluate the right-hand side expression to get a value, then write that value into the memory cell bound to the name on the left. The previous cell contents are overwritten and lost. The name→address binding does not change. When = appears in a TypeScript expression, read it as “gets”, not “equals” — it is a write command, not a mathematical statement. let declares a variable and allows future re-assignment. const performs exactly one assignment at declaration time and seals the binding against further writes. Expressions on the right side involving the same variable (x = x + 1) are safe: the right side is fully evaluated before any write occurs.

Continue the climb ↑Mutation and state
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.