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
?
You: What is
name
? (*)JavaScript:
name
? What's aname
? I don't know what you're talking about. You haven't ever mentioned anyname
before. Are you seeing some other scripting language on the (client-)side?You: What is
name
?JavaScript: I don't know.
In short;
undefined
is where no notion of the thing exists; it has no type, and it's never been referenced before in that scope;null
is where the thing is known to exist, but it's not known what the value is.One thing to remember is that
null
is not, conceptually, the same asfalse
or""
or such, even if they equate after type casting, i.e.You: What is
name
?JavaScript: Boolean false.
You: What is
name
?JavaScript: Empty string
*:
name
in this context is meant as a variable which has never been defined. It could be be any undefined variable. However, name is a property of just about any HTML form element. It goes way, way back and was instituted well before id. It is useful because ids must be unique but names to not have to be.Some precisions:
null and undefined are two different values. One is representing the absence of a value for a name and the other is representing the absence of a name.
What happens in an
if
goes as follows forif( o )
:The expression in the parentheses o is evaluated, and then the
if
kicks in type-coercing the value of the expression in the parentheses - in our caseo
.Falsy (that will get coerced to false) values in JavaScript are: '', null, undefined, 0, and false.
The best way to think about 'null' is to recall how the similar concept is used in databases, where it indicates that a field contains "no value at all."
This is a very useful technique for writing programs that are more-easily debugged. An 'undefined' variable might be the result of a bug ... (how would you know?) ... but if the variable contains the value 'null,' you know that "someone, somewhere in this program, set it to 'null.'" Therefore, I suggest that, when you need to get rid of the value of a variable, don't "delete" ... set it to 'null.' The old value will be orphaned and soon will be garbage-collected; the new value is, "there is no value (now)." In both cases, the variable's state is certain: "it obviously, deliberately, got that way."
2.Undefined is a type itself while Null is an object.
3.Javascript can itself initialize any unassigned variable to undefined but it can never set value of a variable to null. This has to be done programatically.
From "The Principles of Object-Oriented Javascript" by Nicholas C. Zakas
Zakas, Nicholas C. (2014-02-07). The Principles of Object-Oriented JavaScript (Kindle Locations 226-227). No Starch Press. Kindle Edition.
That said:
Undefined case:
First part of the question:
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:
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("")
, ornew 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:
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)
andif(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, likevar 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
And maybe all should throw exceptions.