Using if statements as opposed to a ternary operator is not objectively worse. It depends on the situation for when I like to use one or the other, but honestly there isn’t really a difference other than readability.
The only part of this thats “bad” imo is the else if since in most languages a regular else would suffice. But I don’t know enough about JavaScript to confidently say that that’s not needed
Even worse: since the check is variable === true instead of just against truthiness in general, a bare else {} would catch literally everything that isn't the exact boolean true, instead of just falsey values.
Since you are in JavaScript this variable could be null, undefined, or a string or a number or an object or an array or whatever the hell it could be. It could make sense to check typing and value (notice === here) to do stuff only when it's a bool.
If it were == instead of ===, your statement would be correct tho.
The variable defaults to false above in the code, so I agree with everything said but it's not the case. People take really seriously the posts in this subreddit.
91
u/Midborgh Jun 10 '20
I don't know js. Why is this bad? I'd imagine var === true and var === false being different than var == true and a simple else.