Why null is considered an object in javascript?
Is checking
if ( object == null )
do something
same as
if ( !object )
do something
And also
What is the difference between null and undefined??
Why null is considered an object in javascript?
Is checking
if ( object == null )
do something
same as
if ( !object )
do something
And also
What is the difference between null and undefined??
null is an object. It's type is null. undefined is not an object, it's type is undefined.
What is the difference between null and undefined??
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:
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
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 checking if (o == null)
for o
being false
.
var x = null;
x is defined as null
y is not defined; // because I did not define it
if (!x)
null is evaluated as false
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.
The difference can be summarize 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 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
alert(Boolean(null)) //false
alert(Boolean(0)) //false
alert(Boolean("")) //false
bye ;)
(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
Some precisions,
null and undefined ARE two different values. One representing the absence of value for a name and the other representing the absence of a name. Douglas, if you're out there, correct me if I'm wrong!
What happens in an if goes as follow for if( o )
:
The expression in the parentheses o is evaluated, then the if kicks in type-cohersing the value of the expression in the parentheses - in our case o.
Falsy ( that will get cohersed to false ) values in javascript are: '', null, undefined, 0, false.