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?
if (myBoolean === true)
? That is actually not equivalent toif (myBoolean)
; it's only true ifmyBoolean
is actually set totrue
, making it safer.myBoolean === true
, that'd be a different matter.===
would be different.===
), please do that separately.