Variables in Code
A variable is a named storage spot in a program that holds a piece of information your code can use and change.
Reading is good — doing is better. Practice Variables in code as an interactive lesson.
Try the lessonDefinition
In coding, a variable is like a labeled box. You give the box a name, and then you put a value inside it — like a number, a word, or true/false. Your program can look inside the box, use what is there, or swap it out for something new. Variables let programs remember things while they are running.
Remember the rule
Name it → Store it → Use it. Every variable needs a name, a value, and a place in your code where you actually use it.
Key words
- Variable
- A named place in a program's memory that stores a value, like a box with a label on it.
- Value
- The actual piece of information stored inside a variable, such as the number 10 or the word "hello".
- Declare
- To create a new variable and give it a name for the first time in your code.
- Assign
- To put a value into a variable, using an equals sign ( = ).
- Data type
- The kind of information a variable holds — for example, a whole number, a decimal, a word, or true/false.
- String
- A data type made of letters, words, or symbols — always written inside quotation marks, like "cat" or "hello world".
- Integer
- A data type that is a whole number with no decimal point, like 5 or 42.
- Update
- To replace the old value in a variable with a new one.
Worked examples
You want your program to remember a player's score. How do you make a variable for it?
→ score = 0 Now the variable named score holds the value 0. · Starting a score at 0 before the game begins is called initializing the variable.
The player earns 10 points. How do you update the score variable?
→ score = score + 10 The program reads the old score (0), adds 10, and stores 14 — wait, adds 10, so now score = 10. · The right side is calculated first, then the result is saved back into the variable on the left.
You want to store a player's name. What does that variable look like?
→ player_name = "Alex" The variable player_name now holds the string value Alex. · Words and letters must go inside quotation marks so the computer knows they are text, not code.
You want to keep track of whether a game is over. What kind of variable works best?
→ game_over = False When the player wins or loses, you change it: game_over = True · True/False variables are called Boolean variables — they can only ever be one of those two values.
A program has: apples = 5, then apples = apples + 3. What is the value of apples at the end?
→ apples = 8. The program started with 5, added 3, and saved 8 back into apples. · The old value (5) is gone — the variable now only remembers the newest value (8).
You have two variables: length = 4 and width = 6. How do you store the area of a rectangle?
→ area = length * width area now holds the value 24, because 4 times 6 equals 24. · Variables can be used together in math expressions to calculate new values.
Common mistakes
- Using a variable before declaring it — the program will crash or give an error because it does not know what that name means yet.
- Forgetting quotation marks around text values — writing name = Alex instead of name = "Alex" makes the computer think Alex is another variable, not a word.
- Mixing up the variable name with the value — the name is the label on the box; the value is what is inside. They are not the same thing.
- Thinking the equals sign means 'equal to' like in math — in code, = means 'put this value into this variable', not 'these two things are the same'.
- Accidentally reusing a variable name for a different purpose later in the program, which overwrites the old value and causes confusing bugs.
FAQs
Can a variable hold different kinds of information at different times?
Yes! You can change both the value and sometimes even the type stored in a variable as your program runs. For example, score might hold 0 at first and 150 later.
Does the variable name matter?
The name does not change how the program works mathematically, but a clear name like player_score is much easier to understand than a name like x or z. Good names help you and others read the code.
What happens to a variable when the program stops running?
It disappears. Variables only live in the computer's memory while the program is running. The next time you start the program, variables start fresh from whatever values the code sets them to.
Is there a limit to how many variables I can have?
In most beginner coding environments, you can have as many variables as you need. Just give each one a different name.
Why do some variable names use underscores, like player_name instead of a space?
Computers treat a space as the end of a word, so player name (with a space) would confuse the program. An underscore connects the two words into one name the computer can read.
Can two variables have the same name?
No — if you declare two variables with the exact same name, the second one will overwrite the first, and you will lose the original value. Always give each variable a unique name.
Want the whole picture for your child?
Every K–6 subject, an AI tutor that teaches step by step, unlimited practice, and a reward world.
Start a 3-day free trial