Hey, everyone, in this lesson we are going to talk about no and undefined, so variables not always
have values and no and undefined, they represent the lack of value.
So I'm just going to create a new variable here.
I'm going to name it no value.
And instead of assigning a value to it, I could do something like equals an empty string because maybe
I don't know the value yet.
But instead of doing this, I don't have to assign anything.
I can just create it.
And now I have a variable called No Value.
I can call it.
But as you can see, the value assigned to this variable is undefined.
If I try to do the type of operator to the no value variable, we can see that the type is undefined.
So this is an actual data type.
So other examples of undefined values.
Let's create a variable for a.
First name.
Now, remember when we talked about strings, we said that a string is a sequence of characters.
So we can't access each of these characters with an index.
So the first character is index zero.
This is going to return P.
Then we have the second.
Third, we can go all the way to the end.
And if we try something that does not exist.
So the last index of this string is four.
If we try five.
This is going to return undefined.
So, as you can see, undefined really represents a lack of value.
All right.
So now let's talk about.
No, no, it's not exactly a data type.
And for many people, it should be avoided.
I've just left a link of an article that you can read.
So this guy is saying that you shouldn't use.
No.
And some people argue that you don't really need.
No.
So what is.
No.
Let's say I have a variable.
So I'm going to create a variable called temperature.
And I'm going to assign, let's say, 35 degrees Celsius.
Right.
So now I have the temperature.
If I try to get this, we can see that thirty five has been assigned to it.
So let's say I have a program that gets the temperature every minutes.
So I go and try to get the temperature.
Let's say we now try to get the temperature and we are unable to do it.
We don't know if it's still 35.
We don't know if it changed, but we don't have the actual temperature.
We are trying to get it.
But we did not receive and in fact, maybe there was a problem in the connection with the sensor.
We just know that this time nothing came back.
So instead of leaving the temperature that that it was before, we can set the temperature to know.
So now if we call this variable, we can see that the value assigned to it is no.
So this will tell the programmer that something went wrong.
And then the programmer can write an exception and tell JavaScript what to do when we don't have a temperature
back.
Now, if I try to do the type off operator with the temperature variable, you can see that.
No, it's not actually a data type.
No.
Is actually an object.
So this is why this could be a bit confusing.
But I personally like to use.
No, because by using no, you can differentiate.
One thing is a variable that never had any value assigned to it.
And another thing is a variable that may be already had a value.
But now it doesn't have anymore.
So this is why I like doing it.
So I'm going to create a new variable called Prasher.
I'm not going to assign anything.
So this means that I have never used this variable.
So if I get its value, it's undefined.
I've never used I've never assigned any value.
Another thing is the temperature that already had a value.
But now it's no.
So it's really up to you.
If you want to use no or not.
Because in the end, we could just get the temperature.
If we don't have the temperature, we can just set it to undefined as well.
So this is why a lot of people think that no is really useless and that you should not use it.
But while this is something that you might see on many programs.
So I just wanted to show you that this exists.
But in our examples from now one, we're just going to leave no aside and we are going to use undefined
when we need to represent the lack of value.
So I don't think there is nothing more about new and undefined that I need to say now.
We just know that they exist and we will have opportunities to use them.
So now it's time to do our first list of exercises.
I'll see you in the next video.