TheZver
TheZver

Reputation: 1582

JavaScript if(x) vs if(x==true)

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

if(x){}

and

if(x==true){}

Thanks

Upvotes: 14

Views: 33924

Answers (2)

thefourtheye
thefourtheye

Reputation: 239683

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

Upvotes: 34

Darius
Darius

Reputation: 5269

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

Upvotes: 0

Related Questions