4 ms·
Instead of using === everywhere, if you parse and cast your input to known types you also have the opportunity to catch errors where input is not conforming. Y
by jessedhillon 12y ago
Instead of using === everywhere, if you parse and cast your input to known types you also have the opportunity to catch errors where input is not conforming.
Your parsing makes it obvious to other programmers what your assumptions are about the input. With === all a person will see is that the expected condition did not trigger but no obvious reason why. If `key === 13` fails all we know is that there is no strict equality, but `parseInt(key) == 13` failing means that key is not something which could parse to number 13, and consequently what type(s) key can be is obvious.
Moreover with parseInt the left side of that operator will always be a number or NaN, and the right side is a scalar, so there is no justification for using `parseInt(key) === 13`. It would be preposterous. If you can't trust the return type of parseInt, then your programming environment is unreliable.
- andrewchambers 12y agoI never said a thing about using "===". I think implicit conversion encourages people to use something that is error prone. Tbh, I would suggest using something like typescript and having more granular types than just "number" and "string".