21

When I am using any one of values(null, undefined, false, '', 0) in a if statement, it is always evaluated as fallacy(false). Also, the negation of these values((null, undefined, false, '', 0) in a if statement always evaluated as tautology(true).

 if(null){
 }else{
 }

 if(undefined){
 }else{
 }

 if(false){
 }else{
 }

 if(''){
 }else{
 }

 if(0){
 }else{
 }

In all the above cases, if statement is evaluated as false & else statement executes. However, when I am comparing these fallacy values with == operator, it is not returning true always. Surprisingly, it is always returning true values when I am comparing the negation of these values.

if double equalto (==) operator checks/compares for values & not strictly for types, then why:

null == false          // returns false

null == 0              // returns false

null == ''             // returns false

But,

!null == !false       // returns true

!null == !0           // returns true

!false == !undefined  // returns true

And,

null == undefined     // returns true

false == 0            // returns true

I appreciate if any one can clarify the behavior or relationship among these values(null, undefined, false, '', 0).

Suraj Kumar
  • 564
  • 1
  • 3
  • 11
  • 7
    [Here is the `==` operator as defined in the spec.](http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3) – Pointy Jan 18 '14 at 15:42
  • 1
    Since nobody has mentioned it here, the double exclamation (!!, i.e. not not) can be useful. if (!!myvariable) returns true if myvariable's value is not any value that makes it false. This is not always the same as checking if (myvariable == true). – Doug Leary Feb 12 '15 at 09:15

3 Answers3

17

A common misconception

"...If double equalto (==) operator only checks/compares for values & not for types..."

That's an incorrect assumption, though it's often repeated by people. In reality, the == does check types, and in fact pays far more attention to the types than a === comparison does.

See Abstract Equality Comparison Algorithm.

A == comparison doesn't do a simple toBoolean conversion. Rather it walks through a somewhat complex recursive algorithm, which, after checking the types, attempts to coerce the operands to the same type if they don't match.

The type coercion that it performs is very specific to the types of the operands. A different sequence of coercions can take place for different type pairs. Usually (but not always) it ends up ultimately coercing the operands down to number types.


Why !ing the operands changes things

When you manually coerce both operands using !, you're now doing a simple toBoolean conversion causing the types to match, which avoids the type coercive part of the algorithm, making it behave essentially like the Strict Equality Comparison Algorithm.

So the only way to predict the outcome of a == comparison when the types don't match is to understand that Abstract algorithm.


Don't forget about NaN

And FYI, there's one more "falsey" value to consider, NaN. Its == comparison will always be false, no matter what. Even when comparing to another NaN value, it'll be false.

cookie monster
  • 10,671
  • 4
  • 31
  • 45
1

undefined: means a variable was declared but has no value assigned

null: the value of null has been assigned, which means it has no value

false, '' and 0 I think you can probably work out what these mean.

Todd Motto
  • 903
  • 6
  • 10
1

NULL is different from false (NULL is of type object and false is of type of boolean), null is different from 0 (0 is of type integer), null is also different from '' ('' is of type of string). but they are all falsy values. The ! operator negates a boolean value. If ! is used on falsy values, it results to conversion of falsy values to an object of type boolean.

Allan Chua
  • 9,305
  • 9
  • 41
  • 61
  • Not an "*object* of type boolean", but in general yes. – Bergi Jan 18 '14 at 15:49
  • Thanks for replying. My questions was - if all these values are falsy values(null, undefined, false, '', 0) and double equalto (==) operator compares values(not types) then why they returns - null == false // returns false null == undefined // returns true !null == !false //returns true – Suraj Kumar Jan 18 '14 at 15:55
  • 1
    @SurajKumar, it's because the == operator converts both operands to the same type before performing the actual comparison. – Allan Chua Jan 18 '14 at 15:57