Module 03 / 15

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 You Will Learn
  • 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.
Who Is This For?

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.

Real Life Connection

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.

Two Types of Content

Our boxes can hold different things, but for now, let's look at the big two:

  1. Strings (Text): Words, sentences, or names. They must be wrapped in "quotes".
  2. Numbers: Scores, ages, or prices. No quotes needed!

To create a box, we use the word const (which means "Constant" or "Fixed Box").

The Variable Process
1. Name it
myName
=
2. Fill it
"Rohan"
3. Use it
Logger.log(myName)
Building Your First Box

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."

function heroStats() {
  // Creating a Text box (String)
  const heroName = "Iron Man";

  // Creating a Number box
  const powerLevel = 9000;
}
The Full Script

Copy this into your Google Apps Script editor and click Run:

function learnVariables() { const myName = "Future Coder"; const luckyNumber = 7; // We use the names of the boxes to show the info! Logger.log(myName); Logger.log(luckyNumber); }
The Result

The Execution Log will show the content inside the boxes, not the names of the boxes themselves:

[Execution Log]
Future Coder
7
Don't Fall Into These Traps!
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!

The "Mini Profile" Challenge

Try to create three variables: yourName, yourAge, and yourFavoriteColor. Use Logger.log to display them all. Can you do it?

Who Else Uses This?

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!

Module Summary

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 🗄️

Type NEXT to unlock the next module