5

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?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Vijay Barnwal
  • 144
  • 1
  • 5
  • 3
    Try `===` instead of `==` ;-) – Álvaro González Jul 28 '16 at 08:02
  • datatype of null is "object" whereas for undefined its "undefined" and I think there is no similarity for null and undefined then why its print true? – Vijay Barnwal Jul 28 '16 at 08:03
  • Note the details for the abstract equality operator (or rather just a copy of the specs petty much) is also defined in the [SO Documentation](http://stackoverflow.com/documentation/javascript/208/comparison-operations/796/abstract-equality#t=201607280802422140727). – Spencer Wieczorek Jul 28 '16 at 08:11
  • Hi Alvaro yes I know === compares value as well as datatype but in my case I only check value between null and undefined how is it possible that null and undefined both having same value? Is there any concept or my confusion to understand this? – Vijay Barnwal Jul 28 '16 at 09:13
  • @VijayBarnwal: They don't have the *same* value, but their values are loosely equal according to the definition of loose equality in JavaScript. – T.J. Crowder Jul 28 '16 at 09:18
  • Thanks Spencer I got your answer and really it solve the confusion. – Vijay Barnwal Jul 28 '16 at 09:19
  • Thanks T.J. but can you please elaborate loosely equal word in javascript? – Vijay Barnwal Jul 28 '16 at 09:23

6 Answers6

12

It'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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • This should be the accepted answer. The rules are also stated in [mozila developers network](https://developer.mozilla.org/en/docs/Web/JavaScript/Equality_comparisons_and_sameness) – Kanagu May 19 '17 at 12:44
0

The specifications for the == operator is defined so that null == undefined returns true.

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
0

The spec - Clause 11.9.3 . See clauses 2 and 3.

onefootswill
  • 3,707
  • 6
  • 47
  • 101
-1

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.

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
-1

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?

Community
  • 1
  • 1
Yaelle
  • 401
  • 3
  • 5
-1

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.

Nate Whittaker
  • 1,866
  • 15
  • 14
  • *"Both null and undefined are considered "falsy", therefore they're loosely considered equal."* Nope, lots of falsy values aren't loosely equal to each other. `0 == null` is `false`, `false == undefined` is `false`, `NaN == ""` is `false`, ... – T.J. Crowder May 19 '17 at 13:41