How to Teach Variables to Kids
In most programming languages, a variable is a container that holds information that you can access. You can think of a variable as a box that contains something. You can open the box to find out what is inside, or even put something else into the box. In some ways, human memory and computer memory are similar. We rely on our memory for remembering all sorts of things.
For example, while watching a basketball game, we remember the current score. Now, in computer-speak, that’s really a variable that holds a number such as 84, and changes over time as more baskets are scored. My favorite ice cream flavor? That’s another variable that holds a value such as butterscotch. Variables can hold numbers, fractions, and phrases.
So, how exactly do computers store, organize, and retrieve this information? Let’s take a look at some simple variables!
What is a Variable?
In computer programming, a variable has a name and contains a value. Think of our previous analogy of a variable as being a box. If you labeled the box as Toys and put a yo-yo inside it, in programming terms, Toys is the variable name, and yo-yo is the value.
In most programming languages, a variable can store a single type of value. Usually these are numbers, fractions or phrases. The kind of value that a variable can hold is also called a data type. Now, let us look at 3 different data types – Numbers, Strings, and Booleans.
A number is one of the simplest data types. Programming languages have different kinds of numbers. Integer variables can hold numbers like 5 or -3. For holding fractional values, some programming languages have special types to hold fractional values like 3.14.
For example, here is a variable in use in Tynker:
score is a variable with a value of 0 when the program starts. According to the program above, it looks like every time a candy is found, score is incremented by 1. So, if five candies are found, score will have the value 5.
In the code shown above, there is another variable. Can you find it?
Here, myName and myAge are variables and “Jeremy” and 14 are the values they contain. So the alert command would print:
Hi, I’m Jeremy and I am 14 years old.
Strings are variables used to represent text. Strings hold groups of characters, like a word or a phrase. An easy way to think about strings in real life is to think about how your brain stores information. Think about a person as a variable. A name is simply a value that your brain uses to identify that person.
To practice using strings, suggest these variables that your child can add values to and use them later in a sentence.
What would this sentence be if your values are “Michael,” “bunny,” “Sunday,” and “park”?
“<Name> chased the <Animal> all the way to the <Place> on <Day>”
What if your sentence is:
“The <Animal> chased <Name> all the way to the <Place>!”
A Boolean contains the value true or false. Booleans are truth values–they’re a data type that can represent one of two outcomes: true or false. Booleans answer yes-or-no questions, and Boolean functions are used to perform tests. In a video game, you might test if a player has walked into an obstacle, or if their health is equal to five.
Do this fun unplugged activity with your child to teach them about Booleans! Tell your child to think of their favorite animal (or other favorite thing, such as a favorite food or favorite book. Make sure it’s something that you would know about!). Then, tell your child that you are going to ask them true/false questions to guess what the item is (and that they can’t change the item). After they have thought of the item, ask them a series of true/false questions and try to guess the item. Remind your child that they can only answer true or false! Explain to your child that they are functioning like a computer when they hear your true/false question (the input), think about the right answer, and then provide an output of true or false.
If you are a teacher, you could have students ask each other true/false questions. An alternative activity is to write a variety of questions (not just ones that can be answered by true/false) on index cards. Hold up each card to students and have them identify whether each one can be answered with true or false or not. Reinforce that questions that can be answered by true or false are Boolean expressions.
There are more complex data types beyond numbers, strings, and Booleans. For example, if you want to store a picture, a large document or a list of contacts, you need more complex data types to hold that information. In a later article we’ll get into some of those as well.
In Tynker courses such as Goblin Quest, students learn about these variables and apply them as they build several hands-on coding projects such as Memory Game, MadLibs greeting cards, Geography Quiz, The Never-Ending Story, and more.