Yes, there is a difference. As you already mentioned, if you compare a value with ==
, type conversion takes places.
If the values are not of the same type, they will both be converted to either strings or numbers. If one of the values is a boolean and the other is not, both values will be converted to numbers.
The comparison algorithm is defined in section 11.9.3 of the specification. The important step is here:
7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y)
.
So true
is converted to a number first and later myVar
will be converted to a number as well.
If you only have if(myVar)
though, then the value is converted to a boolean:
2. If ToBoolean(GetValue(exprRef))
is true, then
ToNumber
[spec] and ToBoolean
[spec] can return very different results.
Note: If myVar
is actually a boolean, then there is no difference between if(myVar == true)
and if(myVar)
.