Why is null
considered an object
in JavaScript?
Is checking
if ( object == null )
Do something
the same as
if ( !object )
Do something
?
And also:
What is the difference between null
and undefined
?
Why is null
considered an object
in JavaScript?
Is checking
if ( object == null )
Do something
the same as
if ( !object )
Do something
?
And also:
What is the difference between null
and undefined
?
null and undefined are both false for value equality (null==undefined): they both collapse to boolean false. They are not the same object (null!==undefined).
undefined is a property of the global object ("window" in browsers), but is a primitive type and not an object itself. It's the default value for uninitialized variables and functions ending without a return statement.
null is an instance of Object. null is used for DOM methods that return collection objects to indicate an empty result, which provides a false value without indicating an error.
To add to the answer of What is the differrence between
undefined
andnull
, from JavaScript Definitive Guide on this page:null
is not an object, it is a primitive value. For example, you cannot add properties to it. Sometimes people wrongly assume that it is an object, becausetypeof null
returns"object"
. But that is actually a bug (that might even be fixed in ECMAScript 6).The difference between
null
andundefined
is as follows:undefined
: used by JavaScript and means “no value”. Uninitialized variables, missing parameters and unknown variables have that value.Accessing unknown variables, however, produces an exception:
null
: used by programmers to indicate “no value”, e.g. as a parameter to a function.Examining a variable:
As a general rule, you should always use === and never == in JavaScript (== performs all kinds of conversions that can produce unexpected results). The check
x == null
is an edge case, because it works for bothnull
andundefined
:A common way of checking whether a variable has a value is to convert it to boolean and see whether it is
true
. That conversion is performed by theif
statement and the boolean operator ! (“not”).Drawback of this approach: All of the following values evaluate to
false
, so you have to be careful (e.g., the above checks can’t distinguish betweenundefined
and0
).undefined
,null
false
+0
,-0
,NaN
""
You can test the conversion to boolean by using
Boolean
as a function (normally it is a constructor, to be used withnew
):Comparison of many different null checks in JavaScript:
http://jsfiddle.net/aaronhoffman/DdRHB/5/
http://aaron-hoffman.blogspot.com/2013/04/javascript-null-checking-undefined-and.html
For example
window.someWeirdProperty
is undefined, so"window.someWeirdProperty === null"
evaluates to false while"window.someWeirdProperty === undefined"
evaluates to true.Moreover checkif
if (!o)
is not the same as checkingif (o == null)
foro
beingfalse
.The following function shows why and is capable for working out the difference:
If you call
You're getting
The first
console.log(...)
tries to getmyProperty
frommyObj
while it is not yet defined - so it gets back "undefined". After assigning null to it, the secondconsole.log(...)
returns obviously "null" becausemyProperty
exists, but it has the valuenull
assigned to it.In order to be able to query this difference, JavaScript has
null
andundefined
: Whilenull
is - just like in other languages an object,undefined
cannot be an object because there is no instance (even not anull
instance) available.