Null object in javascript
up vote
209
down vote
favorite
99
|
Why is null considered an object in javascript? Is checking
the same as
And also What is the difference between null and undefined?
javascript object null
| |||
feedback
|
12 Answers
up vote
499
down vote
accepted
|
You: What is
You: What is In short; One thing to remember is that
You: What is
You: What is
| ||||||||||||||||||||
feedback
|
up vote
58
down vote
|
The difference can be summarize into this snippet:
checking object == null is different to check if ( !object ) the latter equals 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
bye ;)
| ||||||
feedback
|
up vote
18
down vote
|
The difference between
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
A common way of checking whether a variable has a value is to convert it to boolean and see whether it is
Drawback of this approach: All of the following values evaluate to
You can test the conversion to boolean by using
| |||
feedback
|
up vote
15
down vote
|
A property when it has no definition, is undefined. null is an object. It's type is null. 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:
Refer to JavaScript Difference between null and undefined for more detail. and with your new edit yes
when testing if object is false, they both only meet the condition when testing if false, but not when true Check here: Javascript gotcha
| ||||||||||||||
feedback
|
up vote
11
down vote
|
Javascript design error they can't fix now. Should have been type null not type object, or not have it at all. Necessitates an extra check (sometimes forgotten) when detecting real objects. Source of bugs.
The two checks are always both false except for:- object is undefined or null: both true. object is primitive, and 0, If object is not primitive but a real Object like So if 'object' is interpreted to mean real Object then both checks are always the same. If primitives are allowed then the checks are different for 0, In cases like
In Javascript, one difference is that null is type object and undefined is type undefined. In Javascript, In Javascript, In reality, null and undefined are identical, since they both represent non-existence. So do 0, and 'false', 'true', and '!' are another bag of worms that could be simplified, e.g. A declared 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.
| ||||||
feedback
|
up vote
7
down vote
|
x is defined as null y is not defined; // because I did not define it
null is evaluated as false
| ||
feedback
|
up vote
5
down vote
|
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.
| ||
feedback
|
up vote
4
down vote
|
null is an object. It's type is null. undefined is not an object, it's type is undefined.
|