Check out a free preview of the full Getting Started with JavaScript, v2 course

The "Values" Lesson is part of the full, Getting Started with JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle presents examples of different kinds of values. Differences are given between primitive and non-primitive values, and between array indexes and object properties.


Transcript from the "Values" Lesson

>> Kyle Simpson: So let's dive in. Values. Here's an example of a variety of values that you can express, again, in a Javascript program. Line one, you see just the number 42. Line two, you see 3.14. Now, there might be a temptation to think that those are different kinds of values because one of them has some decimals on it and one of them doesn't.

In the JavaScript language specifically between numbers that have decimal places after them and numbers that don't. So 42 and 3.14, those are both just numbers. Some languages will distinguish, and they'll say something with a certain number of decimal places is a particular kind of number. But here we're just gonna talk about numbers.

There in line 4 you see, those double quotes that are around it. And by the way you could use double quotes or single quote. So you'll see a variety of different styles of coding as you explore. I just choose to use double quotes to illustrate. This is what we call a string.

A string is a collection of characters that make up something that we can, in English, can actually pronounce, we can say out loud. It's sort of a word or a sentence, a set of characters that go together. And so we're basically saying this is how I can put together something that I might print out a message to the screen, or write some information to a file.

That's why we use a string. Now you notice on line 6 and 7, we have values like true and false. Those two values go together in a sort of kind or a class of value called Boolean. Boolean is actually the name of a famous mathematician from hundreds of years ago, George Boolean.

But we get this idea of true and false and you may of heard that computers work in a bit wise with zeros and ones. There's a same concept here that there is an either one or the other can't be anything other than those two values. So, if you have Boolean, it's either true or false.

And then you notice null and undefined, those two values. Those are two different kinds of what we call empty values. Now, empty value is a little bit strange, like what does it mean for there to not be any value? Or the sort of a placeholder for some missing value, but it's an really important concept in programming.

And each programming language has their sort of own flavor of what it means to have emptiness. Either something that has never been given a value or something that used to have a value and it doesn't anymore. There's sort of an emptiness. It's a little bit of a strange historic fact why JavaScript has two of them.

It might seem like we only need one, like either undefined or null but there's some historical reasons why we end up with two of these. My general recommendation is for you to try to do your programming in such a way that makes those sort of indistinguishable. Don't really try to get too wrapped up in I need a null here but I need an undefined here.

There are a couple of places that you may run into where the distinction is important. But for the most part null and undefined, they're just kinda two different ways of saying. This thing is empty, there is no other value here, so we're just denoting the fact that there's a kind of emptiness to it.

Now lines 12 and 13 get a little bit more interesting. Because on line 12 and 13 we would move out of what we call primitive values, like we've seen thus far, into these non primitive values. And all we mean by primitive is that it is like literally written as an actual value is rather than a collection of values.

So you notice on line 12, that's what we call an array. An array is basically just an ordered set of lists. I have a favorite little anecdote about this. My son, when he was like two or three years old, I got one of these books and it was called A is for array.

It's a great little kids book to teach kids about programming. And it uses the alphabet or signs of different programming term to each letter of the alphabet. And I remember we got as for array, the very first letter and I was trying to help him understand this concept.

They do a good job in the book. What i was trying to help him understand the concept, and what we did was just line up some blocks on the ground. And I was teaching him this whole idea about arrays. And arrays are what we call zero index, meaning the first thing in the array is actually at position zero rather than maybe what we might assume is position one.

There's a long historical reason why that's true in computers. But I was trying to help him understand this idea of zero indexed into an array. And so we lined up several of his blocks and I remember playing with him. And I'd say all right, now pick up block zero, and he would pick up the first one.

Then I'd say pick up block one and he'd pick up the one right next to it. So that's basically what an array is just an ordered collection of these values. And they have these kind of numeric association because of where they are, where is their position. That's how we go and get something.

So if I wanted to get the value 3 out of the array, I would basically need to get the value at position 2 rather than at position 3. And those values, of course, can be anything. I've got numbers here but they could be any of those other values like numbers, strings, booleans.

In fact, they could be even other arrays. Or they could be objects like we see on line 13. Now line 13, it's a little bit different than an array. It's also a collection of values. It's not a primitive single value but a collection of values. But it's a different kind of collection because rather than the values being in positions that are like numbers, like position zero or position one.

These values are at positions that are given names. Almost like you have an address at home, and that's a specific location within your neighborhood and within your city. The mailman knows exactly where to go to deliver the packages or the mail. A key is what it's called, instead of an object, otherwise known as a property.

It's a specific named vocation, and we use a string. So you notice here, I have name. That's the literal key name or property name for this particular position in an object. And I could have either surrounded that property or that key with a string, with the double quotes, or not surrounded it, as I did here.

Most of the time, you don't surround it with quotes, but every once in a while, you need to. And then on the other side of that colon, that's where we have a value. And here I chose to put the value Kyle but of course, it could have been any other value like a number or another array or another object.

So these are the kind of basic building blocks in any sort of programming language. You have values. You need to do things with those values. And we'll get into that in just a little bit. But you have to first understand how to represent some concept that you have in your head.

If you're doing something that is mathematically related, like maybe adding up the amount of money to charge somebody for an order. Of course you need values that are numbers. And if you have a message that you wanna print to the screen, of course you're gonna have strings that you're gonna be manipulating or working with.

Or adding together or moving around or things like that. So these values represent tasks that we want to accomplish. And that have sort of intrinsic behaviors associated with them. You can do math with a number, you can't really do math with a string. That doesn't make sense, but there are operations that you do with a string that don't really make sense for a number.

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