I’m wondering what the core difference is between the conditional syntax below?
if (something) {
// do something
}
vs.
if (something == true) {
// do something
}
Are there any differences?
Edit: I apologize. I made a typo when the question was asked. I did not mean to put a triple equals sign. I know that triple equals is a strict operator. I was meaning to ask if ‘==’ is the equivalent of if (something).
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
somethingis “truthy” while the second will only execute it if it is equal in type and value totrue.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:
false0(zero)""(empty string)nullundefinedNaNAll 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 conditionalThis is wrong. See Felix Kling’s answer.if (something)andif (something == true)are equivalent, though the second is redundant.somethingwill be type coerced in the same way in either case.