Introduction
For many javascript developers, null
and undefined
might look very simple but there might be some questions in your mind about null
and undefined
so, here is a blog explaining all the frequently asked questions about null
and undefined
.
Null
Null is a value which is known to exist but has no valid value has set.
Why null's value is zero?
An interesting fact about null is that, it's value is 0 when performing arithmetic operations,
console.log(null + 5); // 5
Some beginners confuse the value of null with 0
but is it really? No, null
is not 0
, its value becomes 0
when performing calculations.
console.log(+null); // 0
This is because when javascript converts null
to 0
as stated in Annotated ES5 null
is considered as 0
when converted into number while undefined
is converted into NaN
.
Why type of null is object?
So, another weirdest part of javascript is that null's type is object
although it is a primitive.
console.log(typeof null); // "object"
Is there any logical explanation behind this? Well, it is a mistake done in the implementation of early Javascript.
Okay, it is a bug, then why it is not fixed yet? The reason for this is obvious, many old websites which run on old javascript might will face serious errors if typeof null
becomes null
instead of object
and even if the bug is fixed, browser vendors have to implement this specification (Chrome's V8 engine, Firefox's SpiderMonkey, etc).
So, if you want to detect a null
, you can just use a type strict equal operator.
const isNull = n => n === null;
console.log(isNull(0)); // false
Why null == undefined?
Another weird thing thing about null
and undefined
is that null
equals undefined
in javascript,
console.log(null == undefined); // true
Why this now? According to the Javascript Language Specification, if the operands of the equal operator are null
and undefined
, the result would be false. And why is it? I was not able to find any valid reason for this but if you know a valid reason, you can drop your comment so others can know about it.
But, no worries! You can simply use the type strict operator (===
) for this,
console.log(null === undefined); // false
Important Notes about Null
- Null is a falsy value so
Boolean(null) == false
. null
is a reserved keyword
Undefined
Undefined is a value which means the value is not known to exist so, the type of undefined
is undefined
.
let x;
console.log(x); // undefined
console.log(typeof x); // "undefined"
Important Notes about Undefined
- Undefined is a falsy value like
null
soBoolean(undefined) == false
. - While stringifying
undefined
into JSON, it is converted intonull
, therefore,JSON.strinigify(undefined) == "null"
. undefined
is not a reserved keyword so you can define a variable with the nameundefined
but not in global scope.- Unlike
null
, using undefined for arithmetic operations would result inNaN
, since,isNaN(Number(undefined)) == true
.
Null vs Undefined
Another question frequently asked, what is the difference between null
and undefined
?
Undefined | Null |
Undefined is a value which means the value is not known to exist so, the type of undefined is undefined . | Null is a value which is known to exist but has no valid value has set. |
In simple words, undefined is a value which is missing in the compiler (memory). | In simple words, null is used intentionally to express that it is not a valid value. |
Understanding with example
Here are examples how to understand the usage of null
and undefined
.
Here, a non nullish value has returned while accessing the owner
property from DogA
which means the owner of DogA
exists,
const DogA = { name: 'milo', owner: 'sudarsan' };
console.log(DogA.owner); // 'sudarsan'
Here, null
is returned while accessing the owner
property from DogB
which means DogB
can have an owner but has no owner.
const DogB = { name: 'charlie', owner: null };
console.log(DogB.owner); // null
Here, undefined
is returned while accessing the owner
property from DogC
which means DogC
cannot have an owner.
const DogC = { name: 'teddy' };
console.log(DogC.owner); // undefined
The End
If I missed some question about null
or undefined
, let me know in the comments section.
If you find this blog informative,
Thank you for reading till the end!