EDIT: Below only holds true for the original question, in which the ===
operator was used.
The first one will execute the body of the if-statement if something
is "truthy" while the second will only execute it if it is equal in type and value to true
.
So, what is "truthy"? To understand that, you need to know what is its opposite: falsey. All values in JavaScript will be coerced into a Boolean value if placed in a conditional expression. Here's a list of falsey values:
false
0
(zero)
""
(empty string)
null
undefined
NaN
All other values are truthy, though I've probably missed some obscure corner case that someone will point out in the comments.
Here's my answer to the updated question:
The conditional if (something)
and if (something == true)
are equivalent, though the second is redundant. something
will be type coerced in the same way in either case. This is wrong. See Felix Kling's answer.