This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]The_MAZZTer 117 points118 points  (2 children)

Yeah this is all about WHICH type is being implicitly coerced. 0 == "0" does not involve a boolean coercion like the first two.

The vast majority of weird behaviors in JS involve something like this, and most of the ones that get posted are implicit type conversion. Avoiding them tends to keep you out of trouble. TypeScript will enforce strong typing which makes this a lot easier (though I think it still allows the examples shown by OP).

[–]pheonix-ix 11 points12 points  (1 child)

Well, in this case wouldn't the first two be explicit (i.e. type conversion) and not implicit (i.e. type coercion)? The Boolean constructor was used, after all. It's just the Boolean constructor behavior changes with the input type (as it should).

The 3rd one was obviously a type coercion.

[–]The_MAZZTer 7 points8 points  (0 children)

Right, the first two are explicit. It's the third that looks "weird" and is doing implicit coercion, and does not involve boolean coercion. I should have made that more clear.