Why is null
considered an object
in JavaScript?
Is checking
if ( object == null )
Do something
the
First part of the question:
Why is null considered an object in JavaScript?
It is a JavaScript design error they can't fix now. It should have been type null, not type object, or not have it at all. It necessitates an extra check (sometimes forgotten) when detecting real objects and is source of bugs.
Second part of the question:
Is checking
if (object == null)
Do something
the same as
if (!object)
Do something
The two checks are always both false except for:
object is undefined or null: both true.
object is primitive, and 0, ""
, or false: first check false, second true.
If the object is not a primitive, but a real Object, like new Number(0)
, new String("")
, or new Boolean(false)
, then both checks are false.
So if 'object' is interpreted to mean a real Object then both checks are always the same. If primitives are allowed then the checks are different for 0, ""
, and false.
In cases like object==null
, the unobvious results could be a source of bugs. Use of ==
is not recommended ever, use ===
instead.
Third part of the question:
And also:
What is the difference between null and undefined?
In JavaScript, one difference is that null is of type object and undefined is of type undefined.
In JavaScript, null==undefined
is true, and considered equal if type is ignored. Why they decided that, but 0, ""
and false aren't equal, I don't know. It seems to be an arbitrary opinion.
In JavaScript, null===undefined
is not true since the type must be the same in ===
.
In reality, null and undefined are identical, since they both represent non-existence. So do 0, and ""
for that matter too, and maybe the empty containers []
and {}
. So many types of the same nothing are a recipe for bugs. One type or none at all is better. I would try to use as few as possible.
'false', 'true', and '!' are another bag of worms that could be simplified, for example, if(!x)
and if(x)
alone are sufficient, you don't need true and false.
A declared var x
is type undefined if no value is given, but it should be the same as if x was never declared at all. Another bug source is an empty nothing container. So it is best to declare and define it together, like var x=1
.
People are going round and round in circles trying to figure out all these various types of nothing, but it's all just the same thing in complicated different clothes. The reality is
undefined===undeclared===null===0===""===[]==={}===nothing
And maybe all should throw exceptions.
The difference can be summarized into this snippet:
alert(typeof(null)); // object
alert(typeof(undefined)); // undefined
alert(null !== undefined) //true
alert(null == undefined) //true
Checking
object == null
is different to check if ( !object )
.
The latter is equal to ! Boolean(object)
, because the unary !
operator automatically cast the right operand into a Boolean.
Since Boolean(null)
equals false then !false === true
.
So if your object is not null, but false or 0 or "", the check will pass because:
alert(Boolean(null)) //false
alert(Boolean(0)) //false
alert(Boolean("")) //false
Comparison of many different null checks in JavaScript:
http://jsfiddle.net/aaronhoffman/DdRHB/5/
// Variables to test
var myNull = null;
var myObject = {};
var myStringEmpty = "";
var myStringWhiteSpace = " ";
var myStringHello = "hello";
var myIntZero = 0;
var myIntOne = 1;
var myBoolTrue = true;
var myBoolFalse = false;
var myUndefined;
...trim...
http://aaron-hoffman.blogspot.com/2013/04/javascript-null-checking-undefined-and.html
In Javascript null
is not an object
type it is a primitave
type.
What is the difference?
Undefined refers to a pointer that has not been set.
Null refers to the null pointer for example something has manually set a variable to be of type null
null
is an object. Its type is null. undefined
is not an object; its type is undefined.
What is the difference between null and undefined??
A property when it has no definition, is undefined. null is an object. Its type is object. null is a special value meaning "no value. undefined is not an object, it's type is undefined.
You can declare a variable, set it to null, and the behavior is identical except that you'll see "null" printed out versus "undefined". You can even compare a variable that is undefined to null or vice versa, and the condition will be true:
undefined == null
null == undefined
Refer to JavaScript Difference between null and undefined for more detail.
and with your new edit yes
if (object == null) does mean the same if(!object)
when testing if object is false, they both only meet the condition when testing if false, but not when true
Check here: Javascript gotcha