I mentioned when we were talking about primitives and first introduced Nolan undefined, that we would visit them again. Well, now is the time. Null and undefined. Both mean nothingness. So they're a way in JavaScript indicate that nothing is in a particular variable. But what's the difference between the two?
Well, undefined is a state that is established by the system itself. So if we declare a variable but do not assign a value to it, it is undefined. Where null is the state that we would assign as a programmer if we want it to indicate nothing us. Let's look at some examples. So first, let's take a look at undefined if I create a variable current user Don't set it to a value. When I reference current user, it returns undefined.
Now that is different than a variable that has never been declared before. For example, let's see if we can find out what user contains user is a variable i have not declared yet. Notice we get an error at that time. A reference error basically tells us that user is not defined yet. So JavaScript knows the current user has been defined. But there's no value in it.
Therefore it indicates that undefined is its value. Now, how is no different than that? Well, let's say that we have now given current user value, say this is a web application. The current user is now Steven So when we reference current user, that's what it shows us. But let's say that Stephen has left that the current user is left. And so we need to indicate that there is no current user a state of nothingness.
So to do that, we would set that equal to null. Now when we reference current user, it returns null. So that's what I mean by the difference between the system assigning value as nothingness or undefined, or the programmer or the program itself defining the value as null. And that's a great way to keep the difference between the two, undefined and null. It's important to understand that difference as there are situations where in your programming code you need to check to see if there is a value In a variable and if that value is undefined you know it has not been established yet and if is set to null then you know that you have you have set it to nothing as it was something previously but now the value is no longer there. Alright, let's move on to the next topic.