Basic

Variables and Values

Store and name data using variables, and understand the core value types, string, number, boolean, that almost every game tracks, like score, speed, and whether the player is alive.

What you will see

ReferenceError: Score is not defined

About 4 min of reading

Try It

A game has to remember things, how far I have run, how many coins, whether the player is still alive. A variable is just a named box for a value, and once I have one I can read it, change it, and do math with it. Let me create my first box, watch a value move through it, and turn that into something a real game needs.

1

I create my first box with let, bind the number 0 to a name, and read it straight back. The first line puts a value in; the second line takes the same value out and prints it. That read-and-write is the whole point of naming a value.

let score = 0 console.log(score)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
0
let means the box can be refilled later. A box called const would refuse to change, which is what we reach for when a value should never move.
2

Now I refill the box with a new value. The line score = score + 50 reads the old 0, adds 50, and stores 50 back into the same name. The equals sign here is "put this value into", not "these two things are equal". Read it out loud to feel it.

let score = 0 score = score + 50 console.log(score)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
50
The = on the right side of the equation is read as "into": put score plus 50 into the box called score. On the left is where the value lands. This is the single trickiest line for beginners, so read it slowly.
3

I mix a string and a boolean next to score to feel the core value types in action. score is a number, name is a quoted string, and isAlive is true or false. Print all three and watch how each kind shows up, text stays in quotes, the booleans have no quotes at all.

let score = 0 let name = "player" let isAlive = true console.log(score, name, isAlive)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
0 player true
console.log can print several values at once, separated by spaces. Note that name is written without quotes in the output, the quotes were JavaScript syntax, not part of the stored text.
4

Real games change values constantly, so I reassign the box several times with shorthand. score += 25 adds 25, then I subtract a life from lives with -=. Each line reads the old value, mutates it, and stores it back into the same name.

let score = 0 score += 25 score += 25 let lives = 3 lives = lives - 1 console.log(score, lives)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
50 2
lives = lives - 1 is the one that trips people up: it reads the current value, subtracts 1, and writes the result back into the same box. Without reassigning into the same name the number would never change.
5

Names are case-sensitive, so Score and score are two different boxes. I make one mistake on purpose, printing Score instead of score, and watch the engine report that the name does not exist. It is the same honest stop message from the first lesson, a name I never created points to nothing.

let score = 0 console.log(Score)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
ReferenceError: Score is not defined
The box is named score, all lowercase, but Score with a capital S is a different name the engine has never heard. That mismatch throws a ReferenceError, exactly the lesson from 01-first.js.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A value is bound to a name, read back later, and replaced with a new computed value each time it changes.
1 Write a literal (e.g. 0) and bind it to a name with let
↓
2 Later, read the name to use its value
↓
3 Compute a new value from the old one
↓
4 Reassign the name so it points to the new value
↓
5 Repeat for every frame the value changes
Why this works
Why const before let

const means **constant: once you put a value in, you can't change it. That's a feature*: it lets your reader (and the engine) know the value won't move, so you can reason about it more easily.

const highScoreGoal = 1000;   // never changes -> const
let highScore = 0;            // grows over time -> let

Beginners reach for let too much. If you're sure a value never changes, a name, a color, a config number, use const. It catches bugs: trying to reassign a const throws an error instead of silently corrupting data.

Naming matters more than anything

Good names are the most important skill in programming. A variable name should tell you what's inside it.

let score = 0;         // good - you know what this is
let x = 0;             // confusing - x could be anything

Conventions we follow throughout: lowercase, words joined by underscores (highScore, runSpeed); booleans start with is / has / can (isGameOver); and names are case-sensitive.

This exact pair appears in the real game: the game's entry file reads from a central CONFIG object of values like RUN_SPEED that are const because they never change mid-run.

Examples

Named boxes with const and let

A variable is a named box that stores a value. A game needs to remember things, how far the player ran, how many coins, how fast it's going. Give the box a name and you can read or change it later.

const name = "player";   // never changes -> const
let score = 0;           // changes a lot    -> let
score = score + 50;
console.log(score);      // 50

  • const: create a box you won't reassign (a name, a config number).
  • let: create a box you will reassign (a score, a position, a speed).

The rule: reach for const first. Only use let when the value genuinely changes.

The three core types

Not all data is the same. There are a few basic kinds:

console.log("coin");    // string  (text, in quotes)
console.log(300);       // number  (just the digits)
console.log(true);      // boolean (yes / no)

TypeExampleLike this in the game
string"player"a name or label
number300, 3.14speed, distance, score
booleantrueis the game over?

Booleans conventionally start with is / has / can: isAlive, hasCoin, canJump.

Reassigning with arithmetic

When a variable holds a number in an expression, the computer does the math:

let lives = 3;
lives = lives - 1;     // take the old value, subtract 1, store back
console.log(lives);    // 2
lives += 1;            // shorthand for lives = lives + 1
console.log(lives);    // 3

This line is the tricky one for beginners: lives = lives - 1. The = on the right is read into, "put lives - 1 into the box called lives." The value on the left of = is where it lands; the value on the right is what you compute.

Watch out: Score and score are two different variables. Names are case-sensitive.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Your first program
Previous Lesson
Your first program
Next Lesson
Math and Expressions
Math and Expressions →