Real JavaScript Variables
Teach the robot to remember. Variables are labeled boxes that store names, scores, and powers — fill a box once, and your code can use it everywhere.
What is a variable?
A variable is like a labeled box. You write a name on the outside, put a value inside, and close the lid. From then on, whenever your code uses the name on the label, the computer opens the box and grabs whatever is inside. The label stays the same — but you can swap what's in the box any time.
-
1
Make the box:
let heroName = "Mia";lettells JavaScript "make a new box."heroNameis the label you write on it.=means "put this inside," and"Mia"is the value that goes in the box. -
2
Use the label, get what's inside
say(heroName)makes the robot say Mia — not "heroName." There are no quotes around it, so the computer knows it's a box label and opens the box to find the value. -
3
Why coders use them
Variables let a program remember things — a player's name, a score, a power. And if your hero's name appears 20 times in a game, you only change the box once and every spot updates. Try it: change
"Mia"to your own name and run the code again.
Create and use a variable.
let heroName = "Mia";
let power = "moon jump";
say(heroName);
say(power);
Program result
- Run your code to see output.
Type real JavaScript, then run it.
You stored values in variables and used them in JavaScript output.
Open Variable Builder