The Labeled Box
Think back to middle school algebra when your math teacher wrote an equation on the board like 6 = 2x.
Most people hated algebra because x felt abstract and arbitrary. But x was never a mystery: it was simply a labeled box holding onto the number 3. The actual value was tucked away behind the label x.
In programming, variables work the exact same way. When you create a variable, you are simply sticking a label on a container:
- The Label:
playerHealth - What’s Inside:
100
Whenever the game needs to know your health, it looks up playerHealth. When an enemy hits you or you drink an elixir, the game opens that container and swaps the value inside.
The Two Big Differences from Algebra Class
- Ditch the single letters: Throw out labels like
xandy. Give variables clear names you’ll remember the meaning of weeks and months from now (playerHealth,attackDamage,heroGold).
- Read
=as “is”, never “equals”: In math,=represents a fixed two-way balance. A rigid truth. In code,=is a one-way conveyor belt: calculate whatever is on the right side first, and store the result into the box on the left. The underlying value assigned to the label can change.