In JavaScript , in which cases the following statements won\'t be logically equal ?
if(x){}
and
if(x==true){}
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