In this lesson, we are going to introduce null type and undefined type and then we are going to differentiate these both types with examples.
We are also going to give a quick overview about Boolean data type.
Null Type and Undefined Type
In the last lesson, we have discussed about primitive and its type, so null and undefined are also types of primitive, they both are just value that a variable can be set and they both mean nothingness in a sense.
There are not multiple types of them, there is no numbers and strings.
There is just one word Null OR Undefined and there is a big difference between them, here is code to illustrate the difference between them.
So let’s start with undefined if we have declared a variable but we have not declared its value so it is called Undefined.
So I will show you with example here You can see in below code we have created a variable with the name ‘year’ but we have not stored anything in there.
And you can notice in above example, if I am asking him for ‘year’ back it tells me undefined, it is because we have declared ‘year’ variable but not initialized any value that’s why it’s considered undefined.
Let’s see what happen if I ask for something that does not exist like if I want to ask him for a variable ‘Name’ which we have not yet declared, then it will give an error.
Here is an example below.
You can see here if I type ‘Name’ and hit enter, it gives me an actual error message and if I type ‘year’ and hit enter then it will not give an error because we have already declared ‘year’ variable and it will only give us an undefined message.
Null is another way of expressing nothingness, but it is quite different than undefined because null means something in explicitly empty while undefined means that something does not have a value yet.
So in this example here we have showing a case where you might use null: So here we have using a variable ‘name’ and assigned null to this variable, which express that current ‘name’ have null value, a null mean is that the variable have no value.
Here in the above example, null is assigned to a ‘name’ variable.
You can see we have defined a variable but have not assigned any value to it, so the value is absent.
So the main difference is that NULL is absolutely nothing but UNDEFINED is empty or doesn’t have a value yet.
The next data type is the Boolean and Booleans only have two options:
They are True or Yes
They are False or No
There is no quotes, there is no numbers.
They are either true or false In this lesson, we have discussed null type and undefined type and we have also discussed difference in between them.
We have also discussed a quick overview about Boolean type.