Many beginner programmers write things like if (myBoolean == true) as opposed to if (myBoolean) as they haven’t yet grasped that conditionals don’t need to contain a comparison, merely a boolean. When I noted this on twitter it was suggested to me that in JavaScript there might be a good reason to do this. Is there? We all know JavaScript types are strange, but Google yields no answers on this specific point.
Update: It seems there is no difference, however there would be a difference with if (myBoolean === true). So to clarify the question – what would be the best practice in JavaScript if (myBoolean === true) or if (myBoolean). In what real-world scenario would you be checking === true to something that you don’t know is a boolean? Please answer in this follow-up question: Why is it good practice to use if (myBoolean === true) in JavaScript?
No.
Since you’re using the non-strict equality operator, there is absolutely no difference between:
And:
There would be a difference if you were using the strict equality operator
===andmyBooleanwas not actually a boolean, though.