I have a code below which is not clear to me
var a = null;
if(a==undefined)
alert("true");
else
alert("false");
When I ran above code it alerts true.
Can anybody explain whats the reason or concept behind this?
I have a code below which is not clear to me
var a = null;
if(a==undefined)
alert("true");
else
alert("false");
When I ran above code it alerts true.
Can anybody explain whats the reason or concept behind this?
Share Improve this question edited Jul 28, 2016 at 8:08 T.J. Crowder 1.1m199 gold badges2k silver badges1.9k bronze badges asked Jul 28, 2016 at 8:01 Vijay BarnwalVijay Barnwal 1541 silver badge5 bronze badges 7 | Show 2 more comments7 Answers
Reset to default 16It's true because ==
is the loose equality operator, and null
and undefined
are loosely equal (null == undefined
is true). If you use the strict equality operator, ===
, they are not equal (null === undefined
is false).
Basically, the loose equality operator will coerce its operands if they're of different types (see Abtract Equality Comparison in the spec). 0 == ""
is true, for instance, because if you coerce ""
to a number, it's 0
. The strict equality operator considers operands of different types not equal (see Strict Equality Comparison); it does not coerce.
On browsers, there's a third value that's ==
to null
and undefined
: document.all
. document.all
behaves like undefined
in various specification operations. This is so that really, really old code that was using if (document.all)
or similar to go an IE-specific direction doesn't do that on modern browsers, since the features that they were avoiding exit on IE versions that handle document.all
this way. This is defined in the spec.
The specifications for the ==
operator is defined so that null == undefined
returns true
.
The spec - Clause 11.9.3 . See clauses 2 and 3.
Comparison operators operate by converting operands into numbers before comparing. I understood this by an example, which is: Consider you're writing a program to give access to employees into their account. You ask for credentials and the user input nothing. Ideally, you keep asking until it's provided because null is equal only to undefined under ==, which a special case in comparison operation.
If a variable is pointing to nothing, it's null. That's similar in concept to an undefined variable, which has not yet been initialized.
Javascript interpreted this to make loose equality (==) treat null and undefined the same way. Strict equality (===) is more picky; if you have different data types such as null and undefined, they won't be equal.
See What is the difference between null and undefined in JavaScript? on differences between undefined and null and Which equals operator (== vs ===) should be used in JavaScript comparisons?
undefined
means a variable has not been defined, or given a value. null
is a special object within javascript
.
Comparing the values using equals ==
will always give you true
because they are both basically the same in value (0, nothing, nada, zip)
.
Using strict equals ===
though, will return false
because null is an object
and undefined
is just a blank variable
.
This behavior stems from JavaScript being a loosely-typed language. In the case of equality, this means values of differing data types can be compared.
Both null
and undefined
are considered "falsy", therefore they're loosely considered equal.
Logically, if null == false
and undefined == false
, then null == undefined
.
===
instead of==
;-) – Álvaro González Commented Jul 28, 2016 at 8:02