I'd say that the problem is that strict equality can be well defined for different types (not the same type, then not equals), but relational operators can not be well defined for different types.
Assume we define a strict comparator a <== b
to be typeof a == typeof b && a <= b
. And the same for a >== b
. Then we compare a = "3" and b = 3 and the results are a <== b
false, a >== b
false and a === b
false. Congratulations, you just created a paradox!
Such strict comparator would still mess up things like sorting algorithms, or comparing unexpected values. For example:
for (var i; i <== list.count; i++) {
doStuff(i);
}
Note that the example is mistakenly using list.count
instead of list.length
, which will just return undefined
, which would just return false when compared to i <== undefined
, so the for loop would be entirely skipped to the surprise of the programmer.
It would me much better if JavaScript raised an error on list.count
for Undefined Property, and also if comparing different types.
That's all I can say, comparing across types should raise an exception, just like any other decent language out there. But it doesn't.
This means, that the actual practical solution is to start using preprocessors, or just say "Oh well" and keep typing JavaScript ¯\_(ツ)_/¯