EN
JavaScript - why is null an object and what's the difference between null and undefined?
1 answers
0 points
Why is null
an object type and what's the difference between null
and undefined
?
When I'm checking:
xxxxxxxxxx
1
if ( object == null ) {
2
// some action...
3
}
it's the same as:
xxxxxxxxxx
1
if (!object) {
2
// some action...
3
}
also:
xxxxxxxxxx
1
console.log(typeof null === 'object'); // true
1 answer
0 points
1. undefined
is a primitive value automatically assigned to variables that have just been declared. It is where no notion of the variable exists, it has no type and it's never been referenced before in that scope,
2. null
represents the intentional absence of any object value (e.g the variable is known to exist but it is not known what the value is).
Note:
null
is not the same asfalse
or""
values, even if it's treated as falsy for boolean operations:xxxxxxxxxx
1console.log(false == ''); // true
2console.log(false == null); // false
3console.log('' == null); // false
References
0 commentsShow commentsAdd comment