Create a central gameState object that tracks all game data.
Before we write the most important object in your game, let's understand what "state" means. Watch one or both of these:
Video 1: JavaScript Objects Explained — Programming with Mosh (5 min)
Clear, beginner-friendly explanation of JavaScript objects — the building block your entire game state is built on. Watch how keys and values work together.
Video 2: JavaScript in 100 Seconds — Fireship (2 min)
Quick refresher on JavaScript itself — what it can do and why it's the language of the web. If yesterday's coding felt unfamiliar, this will help.
Discussion after watching: If your game was a person, what would it need to "remember"? Its score? Its health? Where the player is? All of that goes in one object called gameState.
State = everything the game needs to remember right now.
A game needs a single source of truth — one object that holds all the data about what's happening right now. This is called "state." Every action in the game reads from or writes to this state object.
| Without State | With State |
|---|---|
| Score is in one variable, health in another, zone in a third | Everything lives in gameState |
| Hard to find bugs — which variable is wrong? | One place to look: console.log(gameState) |
| Different parts of the code might disagree about the score | Everyone reads from the same object — no disagreements |
The Golden Rule: If you want to know anything about the game, the answer is in gameState. Always. No exceptions.
Ami's gameState includes:
xp(number) — current unbanked XPbanked(number) — safely stored XPriskLevel(number) — danger multiplierzone(string: "safe" or "danger") — current locationtier(number) — player progression tier
Ida's gameState includes:
karma(number) — currency earned from buildsdebt(number) — borrowed funds with interesttier(number) — player progression tierzone(string: "on-lot" or "off-lot") — current locationactiveJob(object or null) — current construction job
Open your browser's console (F12 → Console tab) and type each line one at a time. Press Enter after each one.
Step 1: Create an object
Step 2: Read values
Step 3: Change values
Step 4: Add new values
Step 5: Now try YOUR game's state
Challenge: Type gameState.xp += 10 five times. What is gameState.xp now? If you said 100, you understand state. The object remembers everything you do to it.
Clicking buttons updates state. Console shows correct values after each action.
No rendering. No visuals. State is invisible — we see it only in the console. That is correct for today.
Creating multiple state objects instead of one — there must be exactly one gameState. Everything reads from it.
Forgetting to update the state before logging — always change the value first, then log.
Not understanding that state is just a JavaScript object — it's a plain object with keys and values. Nothing magical.
This is the most important architectural concept in the course. Spend extra time here. If the kids only understand one thing today, it should be: "All game data lives in one object."
The Console Playground activity is crucial. Let them type in the browser console freely — there's nothing they can break. If they get curious and try things not in the instructions, encourage it. That's exploration.
Ask: "If I asked you what the player's XP is, where would you look?" The answer must always be: "gameState." If they understand this, everything else flows from it.
Common confusion: kids might think buttons DO things. They don't — buttons CHANGE STATE, and later we'll make the screen SHOW state. Today, the console is how we see that state changed. This is a deliberate separation.
If a child asks "but when do we see it on screen?" — great question! Tell them: "Tomorrow. Today we prove the brain works. Tomorrow we give it a face."
Looking ahead: Tomorrow we wire buttons to this state and make a playable text game. You'll play through 3 full loops of your game using just buttons and console messages.