Differences Between Undefined vs Null
|console.log (typeof null);
Console.log (typeof test);
One can also declare a variable and then assign “undefined” to it like below:
|var test = undefined;
Head to Head Comparison Between Undefined and Null
Let us discuss some of the major Differences Between Undefined and Null:
- Undefined means a variable has been declared but not yet been assigned a value.
- “null” is an assignment value that means “no value”.
- “undefined” and “null” both are primitives.
- “undefined” is of type undefined.
- “null” is of type object.
- One can check if a variable is undefined using: type of variable === “undefined.”
- Way to check if a variable is null using : variable === null
- Equality operator will treat them as equal whereas Identity operator won’t treat them as equal. null === undefined //false null == undefined //true
- Null is written with a literal: “null”. It is not an identifier for a global object’s property like “undefined” can be. “null” gives the lack of identification, means that a variable point to no object.
- When one performs arithmetic conversion on “null”, value determined is 0, this conversion can be verified: var v1 = 3+null; console.log (v1); //3
- “undefined” does not carry out arithmetic conversion like “null” does; if we try to add it to a numeral, you will get NaN (Not-a-Number) error.
Comparison Table Undefined and Null
|BASIS of Comparison Between Undefined vs Null||Undefined||Null|
|Definition||Variable has been declared but not yet been assigned a value||assignment value that means “no value.”|
|Nature||Variable declared but not yet assigned||Represent intentional absence of object value|
|Check||typeof variableName === “undefined”||variableName === null|
|Arithmetic||Not-a-number (NaN) error||treated as zero value|
|Comparison||Equality operator will return true||Identity operator will return false|
|Identifier||Can be an identifier for a property of a global object||Not an identifier for a property of the global object|
Most of the time, people misunderstand the difference between Undefined vs Null. If the distinction between Undefined vs Null remains unclear, it can lead to certain test cases issues.
A variable can be said to be “undefined” if it is declared, but no value has been given to it. On the other hand, “null” is a value assigned to a variable and represents “no value”. Therefore “undefined” is a variable type, where “null” is an object value.
“null” is considered as a place-holder for nothing. It means we have intentionally assigned a value to a variable and thus assuming the value of nothing to a variable. When checking for null or undefined, one needs to be aware of equality (==) and identity (===) operators, as former perform type conversion.
|typeof null //object
type of undefined // undefined
null === undefined //false
null == undefined //true
null == null //true
null === null //true
isNaN ( 1 + null ) //false
isNaN ( 1 + undefined ) //true
So, when it comes to the difference in type, “null” is an object with a valid value having no properties, is non-mutable, and a single instance exists in the system all the time. One can verify the nature of “null” by using the “type of” operator. The use of this operator will give the output as “object”. If we use the “type of” operator on an object that belongs to all criterion of an undefined list, we will receive object type as “undefined”.
Another major difference between Undefined vs Null can be concluded with conversion to primitive types. The way both are converted to primitive types is the key area for differentiation. While performing the arithmetic conversion on “null”, the value determined is zero. However, “undefined” does not carry such conversion. If we try to add “undefined” to a numeral, you will get a Not-a-number error.
Usage of “null” can be very handy in the real world scenario. For example – Some people don’t have a middle name. So, in this case, it is better to assign a value null to a middle name variable in a person object. If someone is accessing the middle name variable in person object and it has value “undefined”. There is no way to ascertain that the developer forgot to initialize this variable or did not have any value. If it is assigned as null, it means a user can easily infer that the middle name variable does not have any value.
This has been a guide to the top difference between Undefined vs Null. Here we have discussed Undefined vs Null head to head comparison, key differences, along with infographics and a comparison table. You may also have a look at the following articles –