First of all, the facts:
if (booleanValue)
Will satisfy the if for any true booleanValue , including true , any non-zero number, any non-empty string value, any reference to an object or array, etc.
On the other hand:
if (booleanValue === true)
This will only satisfy the if condition if booleanValue exactly true . No other true meaning will satisfy it.
On the other hand, if you do this:
if (someVar == true)
Then, what Javascript will do is coerce true to match someVar , and then compare the two variables. There are many situations where this is most likely not what one might intend. Because of this, in most cases, you need to avoid == , because there is a rather long set of rules on how Javascript will introduce coercion so that two things are of the same type, and if you do not understand all these rules and cannot foresee everything, what JS interprets can occur when using two different types (which most JS developers cannot), you probably want to completely avoid == .
As an example of how confusing it can be:
var x; x = 0; console.log(x == true);
For a value of 2 you might think that 2 is a true value, so it compares favorably with true , but that's not how the forced type action works. It converts the value of the right hand according to the type of value of the left hand, so it converts true to the number 1 , so it compares 2 == 1 , which is certainly not what you expected.
So, buyer beware. It is probably best to avoid == almost all cases unless you explicitly know the types to be compared and know how all possible type-coercion algorithms work.
So this really depends on the expected booleanValue values ββand how you want the code to work. If you know in advance that it will ever be true or false , then explicitly comparing it to
if (booleanValue === true)
is just extra code and unnecessary and
if (booleanValue)
more compact and possibly cleaner / better.
If, on the other hand, you do not know what a booleanValue can be, and you want to check if it is really set to true without any other automatic type conversions, then
if (booleanValue === true)
- This is not only a good idea, but also a required one.
For example, if you look at the .on() implementation in jQuery, it has an optional return value. If the callback returns false , then jQuery will automatically stop propagating the event. In this particular case, since jQuery wants ONLY to stop propagating if false was returned, they check the expression for the return value for === false , because they do not want undefined or 0 or "" or anything else that automatically converts the type false to also satisfy the comparison.
For example, here is the jQuery event handling callback code:
ret = ( specialHandle || handleObj.handler ).apply( matched.elem, args ); if ( ret !== undefined ) { event.result = ret; if ( ret === false ) { event.preventDefault(); event.stopPropagation(); } }
You can see that jQuery is explicitly looking for ret === false .
But there are also many other places in jQuery code where a simpler check is appropriate given the desire of the code. For example:
// The DOM ready check for Internet Explorer function doScrollCheck() { if ( jQuery.isReady ) { return; } ...