In the first video on Javascript primitives when we talked about the five basic data types which are
string number Puli an undefined No we kind of lumped non undefined together at the end and I mentioned
that we would cover them in more detail in a future video.
So that's what we're going to do here.
And it's still going to be a pretty quick video because there's actually not very much to know and undefined
.
They are both just values that a variable can be set to and they both mean nothingness in a sense.
But there's a big difference between them.
So the code I have here illustrates that difference.
So let's start with undefined.
So if we declare a variable like we have here var age but we never said it to something.
So it's declared but not initialized to a value it's considered undefined.
So I'll show you what that means.
It looks like if I just run that code var age and I never set it to a value I tell javascript make some
space for something called age but we never store anything in there.
If I asked for age back it tells me undefined and I want to contrast that with if I just ask for something
that doesn't exist at all.
Like color.
It gives me an actual error message versus age.
It knows about age.
It just doesn't know anything inside of age and then no.
It's another way of expressing nothingness but it's very different because no means something is explicitly
empty or explicitly nothingness versus undefined which just means that something doesn't have a value
yet.
So in this example here I'm showing a case where you might use NULL.
So if we had a current player variable we were making a game and it's set to be a string of the name
of the current player.
If that player then dies there's a game over we could set current player to be No.
To make it explicitly clear that there is no current player so direct this appear no undefined are similar
in that they both mean nothingness.
But there's a big difference in that no means explicitly nothing versus undefined means that something
is empty or doesn't have a value yet.
So that really doesn't sound like a big difference.
But as we go throughout this course you'll run into an undefined and we'll see the difference and it
will make a lot more sense as we keep going.
But I just want you to be aware that there is a difference