loading...

Explain variables in programming like I'm five

michael profile image Michael Lee 🍕 ・1 min read

Hey hey friends! Variables are one of the basic concepts you learn in programming. We use them all the time without thinking twice about them. So humor me and explain variables to me like I'm five.

Discussion

pic
Editor guide
Collapse
foresthoffman profile image
Forest Hoffman

I can certainly take a crack at it.

Variables are words/labels that are used to represent information.

Think about what you ate for lunch. For example, let's pretend that you ate a hamburger for lunch. Now, you could tell me that, "I just ate lunch." I would then know that you ate lunch, but I have no idea what you ate. Of course, you would know that you ate a hamburger.

Let's fast forward a day, and you've yet again had lunch. This time you had a salad. You yet again tell me that "I ate lunch!" And yet again, I know that you're referring to some meal you ate in the middle of the day, but I don't know what you ate. Of course, you would know that you ate a salad.

You are able to use the word "lunch" to explain to me that you ate food at some time in the middle of the day. The meaning of "lunch" to me doesn't change. However, the meaning of the word "lunch", to you, changes everyday. That is, unless you eat the same thing several days in a row.

In this analogy, the word "lunch" represents the meal that you consumed on a specific afternoon. It can change at any time. That makes it a variable.

Variables make it easy to refer to information via some kind of label, such as "lunch". You could say to me that you had "lunch" on each day. Or, on the first day, you could tell me that you ate a hamburger around noon, and on the second day, you could tell me that you ate a salad around noon. Using a variable makes things much simpler, since you don't have to be explicit.

Collapse
iamjoross profile image
Jose Ross Barredo

Awesome abstraction! I would say that if was a five year old, I would get this!

Collapse
foresthoffman profile image
Forest Hoffman

Mission accomplished! :)

Collapse
mohanarpit profile image
Arpit Mohan

This is a great question. Although Forest Hoffman nailed it, I'll still give it a shot.

Think of a variable as a pizza box. Now, the box may contain a pepperoni pizza, chicken pizza or no pizza at all. Just by looking at the box, you don't know what's in it. The only way to find out is to actually open the box.
Now, if I ask you to bring me the pizza from the refrigerator, you'll pick up the pizza box and hand it over to me. The box allows you to conveniently move the pizza from one location to another. It gives both of us a handy way to refer to the pizza and also move it around. So, the box is the variable and the pepperoni is the value of the variable.

Collapse
foresthoffman profile image
Forest Hoffman

That's a good way to explain it too. I think you could even extend this to talk about data types.

Collapse
mohanarpit profile image
Arpit Mohan

Thanks. That was the original intention. Wanted to also add the different types of boxes such as pizza box, cake box etc to show different data types. But then, I thought it would complicate the answer, so chickened out at the end. :D

Thread Thread
michael profile image
Michael Lee 🍕 Author

Please do elaborate with the different boxes. Think it's helpful in visualizing how variables work. Plus you did an awesome job in illustrating with pizza. Pizza speaks dearly to my heart.

Collapse
lpasqualis profile image
Lorenzo Pasqualis

A variable is a box with a name and commitment problems.

Collapse
michael profile image
Michael Lee 🍕 Author

Thanks Lorenzo for answering :) Was curious what you meant by commitment problems?

Collapse
lpasqualis profile image
Lorenzo Pasqualis

It was just a joke :) Since a variable can change value, the box is not committed to its content :)