2

I was wondering if in a weakly typed language such as javascript, is there a significant difference between using

var someBool=true;
if (someBool==true){
    //Somecode
}

Or just using

if(someBool){
  //Somecode
} 

Is there a difference in the speed of execution, or is there some semantic difference, or is one just preferred over the other?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
  • Do you already know that the expression can only be true or false? – Eric Lippert Feb 26 '13 at 15:18
  • 2
    I am not sure in a boolean context, but always use === in JavaScript so you have proper coercion. – theshadowmonkey Feb 26 '13 at 15:18
  • @ Eric. No I don't, that was just an example – scrblnrd3 Feb 26 '13 at 15:19
  • @scrblnrd3 then as eric pointed out, you need to use === instead of if(someBool) so you have proper conditional – theshadowmonkey Feb 26 '13 at 15:22
  • In my opinion, explicit comparison to boolean constants in *any* language (well, OK, any traditional C-like language, or anything similar) is an anti-pattern. If the language has statement syntax that expects a boolean-valued expression, then comparisons to boolean constants are fishy at best. – Pointy Feb 26 '13 at 15:24

3 Answers3

4

The semantic differences are considerable, and they are as follows:

if (x)

has the semantics:

  • Determine the value of x.
  • If the value is undefined, null, zero, NaN, an empty string or false then the condition is considered to be false, otherwise it is considered to be true.

if (x==true)

has the semantics:

  • Determine the value of x.
  • If the value is true then the condition is true.
  • If the value is false then the condition is false.
  • Otherwise, pretend that the user actually wrote

if (x==1)

and start over.

I'm not going to go through all the logic for determining if an expression is equal to 1; you can read section 11.9.3 of Revision 3 of the ECMAScript specification if you want the details.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
2

Yes, there is a little semantic difference. The == comparison does more if you have different types of values (e.g. JavaScript truthiness in boolean to numbers comparison, What Are the Semantics of Javascripts If Statement), while if only checks for truthiness.

Yet, if someBool is always known to be a boolean value you should not compare against true, but always use the second variant.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

If you want to check that someBool is in fact true, use the strict equality comparator (===):

var someBool = 1;

someBool == true;  // true
someBool === true; // false

This also works for other types:

0 == "0";  // true
0 === "0"; // false

As others have pointed out, there is a difference between testing for equality (using ==) or testing for "truthyness" (using just if()). It's definitely worth knowing about this, but personally I try to avoid writing code that is not 100% clear in its meaning. Given the name of the variable (someBool), I would expect a boolean, so I would test for a boolean. If I wanted to know if it was greater than 0, I would test for that instead (someNumber >== 0).

As for performance; the strict equality comparator is usually a bit faster than using ==, but the difference is negligible. Testing for truthyness might be a bit faster, but this is still a micro-optimization, which is rarely worth the trouble.

  • `someBool = true; (someBool == 2)` will return `false`, I think that's a big difference. – Teemu Feb 26 '13 at 15:30
  • `==` checks for equality, not for truthiness. For example, `2 == true` is `false` while both are truthy. – Bergi Feb 26 '13 at 15:31
  • @Teemu: Yes, but you would never use that in your code. It's just too fragile and too easily misunderstood by other programmers (or your future self). – Peter-Paul van Gemerden Feb 26 '13 at 15:32