Variables are Magic Boxes!
What Makes a Variable Like a Magic Box?
A variable in coding is likened to a box — each box is labeled with a name and stores things inside it. The box named myAge holds the number 10. The box named myName holds Sam.
Where Do You See Variables in Real Life?
- BackpackA backpack changes what it carries every day.
- Name tagA name tag holds your name for everyone to see.
- Score boardA score board tracks a score that changes during a game.
How Do You Create a Variable in Coding?
In coding, you use let to declare a variable: let favoriteColor = "blue".
let favoriteColor = "blue";
- let — creates the box
- favoriteColor — names the box
- "blue" — the value stored inside
Can You Change What a Variable Stores?
Variables work like a toy box — toys go in and come out freely. The score initializes to 0 at the start, changes to 10 when you score, then changes to 25 as you earn more points.
let score = 0;
score = 10;
score = 25;
What Types of Information Can a Variable Hold?
Variables hold different types of information:
- Numbers — age = 10
- Words (text) — name = "Emma"
- Yes or No — isHappy = true
- Lists — pets = ["dog", "cat"]
How Do Variables Work in a Video Game?
Variables are used in every video game to track changing data:
- Player health decreases when the player gets hit
- Coins collected increases when the player finds coins
- Level number changes when the player beats a level
- Player name stays the same throughout the game
What Are the Key Facts to Remember About Variables?
Key facts
- Variables are compared to labeled boxes that store information
- Every variable is identified by a name — score is an example of a variable name
- Every variable stores a value — 100 is an example of a value
- Variables hold numbers, words, yes/no, and lists