javascriptprogrammingtechnology

Difference Between Undefined and Null Values

Undefined and Null values have few subtle differences. Let’s get to them in this article. They are both primitive data types in JavaScript.
 
Firstly, it’s good to bear in mind that Null and Undefined are different.
Null !== Undefined

Difference #1

Undefined
Undefined means a declared variable without any assigned value. Hence, it is not defined.
Example:
Null
Null means that the variable is empty or non-existent. It is assigned and explicitly means nothing.
Example:

Difference #2

Undefined
Undefined is a falsy value with an undefined typeof.
Example:
Null
Null is a typeof of object.
Example:

Difference #3

Here, JSON categorize Undefined and Null Values. Undefined doesn’t come out in JSON, but null does.

Function parameters are undefined if not passed as argument. Not only that, passing undefined to them doesn’t override their default value:
code

Difference #4

Undefined
Undefined does not strictly equal null but equals null when it’s not strict.
 
Here the values are compared to be equal before checking the data type.
code
Null
Null loosely equals undefined when it’s not strict but does not strictly equal.
code
Here, undefined and null values are compared before we perform type coercion. This means we have to compare both values before we convert them to a common data type.

What's your reaction?

Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0

Leave a reply

Your email address will not be published. Required fields are marked *

Next Article:

0 %