Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Michael Lee 🍕
Michael Lee 🍕

Posted on

     

Explain variables in programming like I'm five

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.

Top comments(10)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
foresthoffman profile image
Forest Hoffman
Software Engineer. Musings about Go, TypeScript, and Node.js. My thoughts are my own.
  • Email
  • Location
    Austin, TX
  • Education
    Associate of Science (2017)
  • Pronouns
    he/him
  • Work
    Senior Software Engineer @ PayPal
  • Joined

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

CollapseExpand
 
iamjoross profile image
Jose Ross Barredo
25. Coder. Noob.INTP-T

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

CollapseExpand
 
foresthoffman profile image
Forest Hoffman
Software Engineer. Musings about Go, TypeScript, and Node.js. My thoughts are my own.
  • Email
  • Location
    Austin, TX
  • Education
    Associate of Science (2017)
  • Pronouns
    he/him
  • Work
    Senior Software Engineer @ PayPal
  • Joined

Mission accomplished! :)

CollapseExpand
 
mohanarpit profile image
Arpit Mohan
Co-founder & CTO, Appsmith.❤️ Distributed Systems. 3X Founder - backed by YC, Sequoia Capital & Accel Partners. Strongly believe in the philosophy “Always be building"
  • Location
    Bengaluru
  • Work
    CTO at Appsmith
  • Joined

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.

CollapseExpand
 
foresthoffman profile image
Forest Hoffman
Software Engineer. Musings about Go, TypeScript, and Node.js. My thoughts are my own.
  • Email
  • Location
    Austin, TX
  • Education
    Associate of Science (2017)
  • Pronouns
    he/him
  • Work
    Senior Software Engineer @ PayPal
  • Joined

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

CollapseExpand
 
mohanarpit profile image
Arpit Mohan
Co-founder & CTO, Appsmith.❤️ Distributed Systems. 3X Founder - backed by YC, Sequoia Capital & Accel Partners. Strongly believe in the philosophy “Always be building"
  • Location
    Bengaluru
  • Work
    CTO at Appsmith
  • Joined

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 🍕
Maker of things, giver of high-fives 🖐
  • Location
    Cary, NC
  • Education
    NC State University
  • Work
    Director of Engineering
  • Joined

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.

CollapseExpand
 
lpasqualis profile image
Lorenzo Pasqualis
I started writing software in 1984. Over the years I worked with many languages, technologies, and tools. I have been in leadership positions since the early 2000s, and in executive roles since 2014.
  • Location
    Seattle, WA
  • Education
    Computer Science
  • Work
    VP of Engineering at DreamBox Learning
  • Joined

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

CollapseExpand
 
michael profile image
Michael Lee 🍕
Maker of things, giver of high-fives 🖐
  • Location
    Cary, NC
  • Education
    NC State University
  • Work
    Director of Engineering
  • Joined

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

CollapseExpand
 
lpasqualis profile image
Lorenzo Pasqualis
I started writing software in 1984. Over the years I worked with many languages, technologies, and tools. I have been in leadership positions since the early 2000s, and in executive roles since 2014.
  • Location
    Seattle, WA
  • Education
    Computer Science
  • Work
    VP of Engineering at DreamBox Learning
  • Joined

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

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Maker of things, giver of high-fives 🖐
  • Location
    Cary, NC
  • Education
    NC State University
  • Work
    Director of Engineering
  • Joined

More fromMichael Lee 🍕

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp