JavaScript: From First Steps to Professional

What are Variables?

Anjana Vakil

Anjana Vakil

Software Engineer & Educator
JavaScript: From First Steps to Professional

Check out a free preview of the full JavaScript: From First Steps to Professional course

The "What are Variables?" Lesson is part of the full, JavaScript: From First Steps to Professional course featured in this preview video. Here's what you'd learn in this lesson:

Anjana discusses a brief overview of what a variable is and provides a suggestion for a mental picture to associate with a JavaScript variable.


Transcript from the "What are Variables?" Lesson

>> Because I have a background in philosophy. I love asking these big questions but what does it all mean man, what really are variables man [LAUGH] so, let's talk about it. Let's talk about variables, what are they? Sometimes you might hear or maybe you've seen if you've encountered other books or courses on JavaScript.

You might hear people describe variables as like boxes that contain values. Have folks, I'm seeing some nods maybe this is ringing some bells for folks. That is one way that sometimes folks find it helpful to think about variables. Personally, I think that and a few other people in the JavaScript community, thinks that that's maybe a little bit misleading to think about variables as containing values because sometimes that can create a mental model in our minds that doesn't really line up with what JavaScript is doing.

Which can trip us up sometimes and cause us to have unexpected things in our programs. So I think it's more useful, and this is why when we were whiteboarding earlier, we talked about variables pointing to values, or you could also say referencing values. In JavaScript, when I name a variable and I assign a value to it, JavaScript is seeing that variable name as a pointer to whatever value I asked it to remember.

Now because I'm sort of old I kind of think about this like speed dial on a phone. Which is the thing that we don't really have anymore because [LAUGH] you we don't need to anymore, but it used to be that you could have a few different speed dial numbers on your landline and you could press one of them like the Simpsons, I guess did.

And get a certain number just dialed for you. Nowadays maybe this is more like adding a contact to your contact book or contact, I don't know app or whatever it is, you're iCloud Drive I don't know [LAUGH]. I think about variables as sort of @!a new contact in our address book that is associated with a particular phone number so like 555555, 5555, I can save that number as a contact in my contact book.

And I could maybe change the name of that contact or maybe my contact changed their phone number and got a new number. What they're not going to do is kind of edit their number a little bit or something like that. So what we really have here is an association between the name that I have this person saved as in my phone and the phone number which is what my phone is actually gonna dial when I try to dial this person using their name and my phone.

When I say hey Siri I don't have an iPhone. But let's say I did, hey Siri, call Stephen. That's going to dial a certain number that is associated with Stephen in my address book. So, this is kind of a mental picture that I use as opposed to the container picture.

Is more of a pointer or an address book kind of picture.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now