Introduction
A variable is a named container that holds a value a program can use and change. Scores, usernames, timers, prices, and progress bars all live in variables. If algorithms are the recipe steps, variables are the labeled bowls holding ingredients as you cook.
This lesson builds on What is Code and algorithms. You will learn how to name storage, put values in, read them back, and update them safely. These ideas appear in loops, functions, and your later mini project. Accurate typing helps when you invent names—keep warming up on practice.
Learning Objectives
By the end of this lesson, you will be able to:
- Define variable, value, and assignment
- Create examples with clear names
- Update a value and predict the new result
- Sort simple values into text, number, or Boolean types
- Explain why variables make programs flexible
Main Lesson
The labeled box metaphor
Imagine boxes on a shelf:
- Box labeled
scoreholds0 - Box labeled
playerNameholds"Amina" - Box labeled
isReadyholdstrue
The name is how the program finds the box. The value is what is inside right now. Assignment means putting a value into the box (or replacing what was there).
Assignment in plain language
In many languages you see patterns like:
```text
score ← 0
playerName ← "Amina"
score ← score + 1
```
Read score ← score + 1 as: take the current score, add one, store the result back into score. After that line, score is 1 if it started at 0.
Why variables matter
Without variables, every value would be hard-coded. Hard-coding Show "Hello, Amina" works once. With playerName, the same greeting program can welcome any student. Variables make programs reusable and interactive.
| Hard-coded approach | Variable approach |
|---|---|
| Always add 10 points | Add bonus points from a setting |
| Always greet "Guest" | Greet using userName |
| Always ask 5 questions | Ask questionCount questions |
| Always use easy mode | Switch using difficulty |
Naming rules beginners should follow
Good names are short but meaningful:
- Prefer
scoreoversorthing1 - Use camelCase (
highScore) or snake_case (high_score) consistently in a project - Names should reflect purpose, not random words
- Avoid spaces and most punctuation in code names
- Do not reuse the same name for unrelated ideas
| Weak name | Stronger name |
|---|---|
x | livesRemaining |
data | emailAddress |
n | numberOfAttempts |
temp (overused) | celsiusReading when that is what it stores |
Simple data types
Values have kinds, often called types:
| Type | Examples | Typical use |
|---|---|---|
| Number (integer/float) | 3, 0, 2.5 | Scores, counts, prices |
| Text / string | "hello", "Grade 6" | Names, messages |
| Boolean | true, false | Yes/no flags, toggles |
| List / array (preview) | [1, 2, 3] | Collections of values |
Beginners mix types by accident—adding a number to text can produce surprises depending on the language. Knowing the type helps you predict behavior and prepare for logic comparisons.
Reading vs writing
- Write / assign — store a value into a variable
- Read / use — look at the value to calculate, display, or decide
Example:
- Assign
attempts ← 0 - Each failed login:
attempts ← attempts + 1 - Read
attemptsin a condition: ifattempts >= 3, lock the form
Constants (intro)
Sometimes a value should not change, like MAX_LIVES ← 3. Some languages have special constant declarations. Mentally treat constants as labeled boxes with tape over the lid—reminder: do not overwrite.
Variables in real products
- A shopping cart total updates as items are added
- A typing test live WPM display recalculates as you type on TYPE10X Practice
- A game health bar decreases when the player is hit
- A form stores
firstNameuntil submit
Behind each changing number on screen is usually a variable updating on a schedule or in response to events.
Key Definitions
- Variable — A named storage location for a value that can be used and often changed.
- Value — The data currently stored in a variable.
- Assignment — Storing a value into a variable.
- Data type — The kind of value (number, text, Boolean, etc.).
- String — Text data.
- Boolean — A true/false value.
- Initialize — Give a variable its first value.
- Update — Change a variable’s value after initialization.
- Constant — A named value intended not to change while the program runs.
- Identifier — The name used to refer to a variable.
Examples
Example 1: Quiz score
score ← 0 → correct answer → score ← score + 1 → display score.
Example 2: Temperature label
celsius ← 22 → message ← "Room is comfortable" if within range → show message.
Example 3: Toggle sound
soundOn ← true → user taps mute → soundOn ← false.
Example 4: Name badge generator
Read firstName and role, then build badgeText ← firstName + " — " + role.
Real-World Scenarios
Scenario A — Sports day app
Students track lap times. Using bestTime and updating only when a new time is smaller teaches comparison plus variables together.
Scenario B — Classroom points
A teacher tool stores classPoints. Without a variable, they would rewrite the whole program each time points change.
Scenario C — Bug from a bad name
A student used score for both quiz points and player health. Updates collided. Renaming to quizScore and health fixed confusion—names are part of correctness.
Tips
Warnings
score vs scores vs Score) can refer to different storage. Stay consistent.Did You Know
Common Mistakes
- Using a variable before giving it a first value
- Choosing cryptic names you forget next week
- Overwriting an important value by accident
- Mixing up the label (name) with the content (value)
- Creating dozens of variables when one well-updated variable would do
Interactive Exercise
Variable Storyboard (10–15 minutes)
Design variables for a simple lunch-order kiosk:
- List at least five variable names and initial values
- Mark each as number, text, or Boolean
- Write three assignment events (example: customer adds a drink)
- Predict final values after those events
Compare with a partner’s design.
Practice Questions
- What is a variable?
- What does assignment mean?
- Rewrite a weak name into a clearer one and explain why.
- Give one Boolean example from school life.
- How do variables make a greeting program more flexible?
Mini Challenge
Invent a “pet status” program plan with variables for petName, hunger (0–10), and isSleeping. Write five algorithm lines that update those variables during a virtual day. Present in 60 seconds.
Summary
Variables are named containers for values programs read and update. Clear names, thoughtful types, and careful assignment turn rigid scripts into flexible tools. Combined with solid algorithms, variables prepare you for decisions and repetition. Next you will study logic in coding—how true/false thinking drives smart behavior.
Student Checklist
- [ ] I can define variable, value, and assignment
- [ ] I can choose clearer names for storage
- [ ] I recognize number, text, and Boolean examples
- [ ] I can update a value and predict the result
- [ ] I completed the storyboard and mini challenge
Teacher Notes
- Use sticky notes as physical “variables” students rewrite with markers.
- Demo a live score counter in Scratch/Python/Blockly if available.
- Collect funny bad names, then rewrite them as a class.
- Tie Boolean variables to upcoming logic and conditions lessons.
- Remind students that typing fluency helps when entering identifiers.
FAQ
Q: Can a variable change type?
Some languages allow it; many discourage it. Beginners should keep one clear type per variable.
Q: How is a variable different from a file?
Variables usually live in short-term working memory while a program runs; files persist on storage. Related, but not the same.
Q: Why do some names look like highScore smashed together?
Spaces break most coding identifiers, so programmers use camelCase or underscores instead.
Q: Do block-coding variables count as “real” variables?
Yes. Same idea—named storage—different interface.
Q: What should I learn next?
Continue to Logic in Coding to combine values with true/false decisions.
Related Lessons
Related Blog Posts
- Keep learning on the TYPE10X Blog
- Improve typing speed for code and notes via Practice
Next Lesson CTA
You can now store and update named values. Next, learn how programs decide what is true or false—continue to Logic in Coding.