Variables: Your Magic Boxes
How to make your computer remember things using labels.
Imagine you are tidying your room. You have a lot of stuff—toys, books, and clothes. To keep them organized, you put them in boxes and write a name on each box. In coding, those boxes are called Variables.
- What a Variable actually is.
- How to store Text (Strings).
- How to store Numbers.
- How to name your boxes so you don't get confused.
If you've ever used a nickname for a friend or labeled a folder on your computer, you already understand variables. We’re just going to show you the "Pro" way to do it.
Think about a Contact List on your phone.
- The Label: "Best Friend"
- The Data: "0123-456-789"
When you want to call them, you don't type the number; you just click the Name. The phone "remembers" the number inside that name.
Our boxes can hold different things, but for now, let's look at the big two:
- Strings (Text): Words, sentences, or names. They must be wrapped in "quotes".
- Numbers: Scores, ages, or prices. No quotes needed!
To create a box, we use the word const (which means "Constant" or "Fixed Box").
myName
"Rohan"
Logger.log(myName)
Let's write a script that stores a superhero name and their power level. We use the = sign. In coding, = doesn't mean "equal to"—it means "Put this value inside that box."
// Creating a Text box (String)
const heroName = "Iron Man";
// Creating a Number box
const powerLevel = 9000;
}
Copy this into your Google Apps Script editor and click Run:
The Execution Log will show the content inside the boxes, not the names of the boxes themselves:
Future Coder
7
Click to reveal common variable errors
1. Forgotten Quotes: If you write const name = John;, the computer thinks John is another variable box. Use "John"!
2. Space in Names: You can't have spaces in box names. const my name is WRONG. Use const myName (This is called camelCase).
3. Numbers First: Variable names can't start with a number. const 1stPlace is bad. const winner1 is good!
Try to create three variables: yourName, yourAge, and yourFavoriteColor. Use Logger.log to display them all. Can you do it?
Apps like YouTube use variables to store the name of the video you are watching and the number of likes it has. Without variables, every video would look the same!
You’ve learned how to store data! Variables (boxes) help your script remember names and numbers so you can use them later in your program. You are building the memory of your app!
Status: Organizer | Level: Data Handler 🗄️