3 ms·
Because the string is first converted to an integer. "==" makes the comparison after converting types (see: Type Juggling in the docs) where "===" requires tha
by cag_ii 15y ago
Because the string is first converted to an integer.
"==" makes the comparison after converting types (see: Type Juggling in the docs) where "===" requires that the types be the same in order to be equal.
Kind of weird the first time you see it, but it's a language design choice and does make sense once you understand it.
- Zak 15y agoI think the reason people object to this behavior, even when they do understand it is that it makes the obvious default (==) dangerous. It's especially dangerous in the hands of the sort of non-experts for whom it's intended to make life easier.
- wvenable 15y ago== really isn't that dangerous unless you use it dangerously. Comparing non-numeric strings to numbers is already odd.
- jessedhillon 15y agoWhat doesn't make sense is the rationale for designing it this way.
- jwatzman 15y agoBut then it means that "==" is no longer transitive, which I see as very unnatural and confusing (even if the underlying reasoning sort-of makes sense).