Why is null
considered an object
in JavaScript?
Is checking
if ( object == null )
Do something
the
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.
(name is undefined)
You: What is name
? (*)
JavaScript: name
? What's a name
? I don't know what you're talking about. You haven't ever mentioned any name
before. Are you seeing some other scripting language on the (client-)side?
name = null;
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 as false
or ""
or such, even if they equate after type casting, i.e.
name = false;
You: What is name
?
JavaScript: Boolean false.
name = '';
You: What is name
?
JavaScript: Empty string
*: name
in this context is meant as a variable which has never been defined. It could 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 do 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 for if( 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 case o
.
Falsy (that will get coerced to false) values in JavaScript are: '', null, undefined, 0, and false.
Use null to define something as having no value, use undefined when you expect something might not be defined at all.
For example, if a variable has no value, assign it as null.
var weDontHaveAValue = null;
If you expect that something might be not defined at all, e.g. an optional options argument, use undefined.
if (typeof args.optionalParam !== 'undefined') { }
To add to the answer of What is the differrence between undefined
and null
, from JavaScript Definitive Guide 6th Edition, p.41 on this page:
You might consider
undefined
to represent system-level, unexpected, or error-like absense of value andnull
to represent program-level, normal, or expected absence of value. If you need to assign one of these values to a variable or property or pass one of these values to a function,null
is almost always the right choice.
The following function shows why and is capable for working out the difference:
function test() {
var myObj = {};
console.log(myObj.myProperty);
myObj.myProperty = null;
console.log(myObj.myProperty);
}
If you call
test();
You're getting
undefined
null
The first console.log(...)
tries to get myProperty
from myObj
while it is not yet defined - so it gets back "undefined". After assigning null to it, the second console.log(...)
returns obviously "null" because myProperty
exists, but it has the value null
assigned to it.
In order to be able to query this difference, JavaScript has null
and undefined
: While null
is - just like in other languages an object, undefined
cannot be an object because there is no instance (even not a null
instance) available.