14

In JavaScript , in which cases the following statements won't be logically equal ?

if(x){}

and

if(x==true){}

Thanks

TheZver
  • 1,502
  • 2
  • 14
  • 19

2 Answers2

34

They are not at all equal.

if (x)

checks if x is Truthy where as the later checks if the Boolean value of x is true.

For example,

var x = {};
if (x) {
    console.log("Truthy");
}
if (x == true) {
    console.log("Equal to true");
}

Not only an object, any string (except an empty string), any number (except 0 (because 0 is Falsy) and 1) will be considered as Truthy, but they will not be equal to true.

As per ECMA 5.1 Standards, in if (x), Truthiness of x will be decided, as per the following table

+-----------------------------------------------------------------------+
| Argument Type | Result                                                |
|:--------------|------------------------------------------------------:|
| Undefined     | false                                                 |
|---------------|-------------------------------------------------------|
| Null          | false                                                 |
|---------------|-------------------------------------------------------|
| Boolean       | The result equals the input argument (no conversion). |
|---------------|-------------------------------------------------------|
| Number        | The result is false if the argument is +0, −0, or NaN;|
|               | otherwise the result is true.                         |
|---------------|-------------------------------------------------------|
| String        | The result is false if the argument is the empty      |
|               | String (its length is zero); otherwise the result is  |
|               | true.                                                 |
|---------------|-------------------------------------------------------|
| Object        | true                                                  |
+-----------------------------------------------------------------------+

Note: The last line object, which includes both objects and Arrays.

But in the later case, as per The Abstract Equality Comparison Algorithm,

If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

value of x will be converted to a number and that number will be checked against true.

Note:

In JavaScript, true is 1 and false is 0.

console.log(1 == true);
# true
console.log(0 == false);
# true
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

Several cases evaluate to false in the first form, such as empty string, 0, undefined, null.

If you want to be a bit more semantic about it, try the bang bang in front of the expression:

if(!!x){...}

this will convert the expression result to a truthy representing the same semantically. This is closer to an analogue to the expression you describe (x == true)

Also be aware that == is value comparions with type coercion, eg "3" == 3, whereas === asserts equal typing too.

So they are not the same, but often logically represent the same test, thanks to the semantics of the language and the !! you can use

Darius
  • 5,180
  • 5
  • 47
  • 62
  • Thanks. is "if(!!x)" always equal to "if(x)" ? – TheZver Apr 14 '14 at 14:05
  • No no, imagine this case if(!!"hello") and if("hello" == true), the first expression will evaluate to true because "hello" is not empty string, undefined or null etc so when converted to truthy, is true. Whereas the second expression will be false, because "hello" cannot be coerced to equal the value true. http://jsfiddle.net/CEtN5/ – Darius Apr 14 '14 at 14:09
  • Sorry I had a mistake in the question. I meant comparison with if(x) – TheZver Apr 14 '14 at 14:10
  • 1
    Ok I see your edit. Essentially you are testing the same thing with if(!!x) and if(x), its just the value of !!x is guaranteed to be true or false, whereas if(x) only works because the language evaluates certain conditions to be true/false, as mentioned earlier, 0, empty string, null, undefined.. which aren't strictly truthy, but can be logically interpreted as such – Darius Apr 14 '14 at 14:12
  • so, to clarify , the following statements are all absolutely equal ? if( x ) , if( !!x ) , if( !!x == true ) , if( !!x === true ) – TheZver Apr 14 '14 at 14:27
  • 1
    they will all evaluate to the same thing, so in that sense they are equal... under the hood their efficiency varies I'd imagine – Darius Apr 14 '14 at 14:56