Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't JavaScript have strict greater/less than comparison operators?

Tags:

javascript

While JavaScript's type-strict comparison operators (===, !==) are nice, it doesn't have corresponding strict comparisons for greater/less than.

var x = 10;

x <= 20;    // true
x <= '20';    // true
x <== 20;   // true (or would be, if JS had such an operator)
x <== '20'; // false (ditto)

Why not? I ask this question fully expecting the answer to be "uh, because it doesn't", but I'm asking anyway, in case there's an interesting and/or disheartening historical reason for such operators being omitted.

like image 821
mwcz Avatar asked Jan 26 '13 01:01

mwcz


People also ask

Why does JavaScript use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...

What is strict comparison in JavaScript?

Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal. If the values have the same type, are not numbers, and have the same value, they're considered equal.

Should I always use === in JavaScript?

The advice given to JavaScript beginners is to completely forget about == and to always use ===. It turns out that that rule is universally true.

How are objects compared when they are checked with the strict equality operator?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.


3 Answers

I can only guess-

If
a === b is false, then
a !== b is true. always.

But, this implication wouldn't hold for <==

If
x <== 20 is false, we cannot infer the result of x >== 20 because it might have been false due to type check, or the relation check.

I think that's slightly confusing, although there's plenty of things in the language that are much worse (type coercion in general, to name one).

However, I think a strict < or > would behave consistently.

like image 172
goat Avatar answered Oct 07 '22 10:10

goat


Since a === b is a shorthand for typeof a == typeof b && a == b, you can use this expansion for inequalities: typeof a == typeof b && a <= b for example.

like image 25
Niet the Dark Absol Avatar answered Oct 07 '22 09:10

Niet the Dark Absol


I'm not sure there is an answer to your question. My guess is, the intended use is for comparing numbers to strings (and maybe booleans). It actually works for those cases, as the non-strict equality operator does. Anything else is subject to arbitrary type coercion rules anyway. What would be the "correct" output of [] < {}? false? Maybe undefined? Note that the types don't even need to be different, ({foo: 1}) < {bar : 2} also doesn't make any sense.

In my opinion, they (Brendan Eich, and later the ECMAScript committee) just decided to trust that the developers would only compare things that make sense comparing. Or didn't even consider that developers would try crazy comparisons. Creating extra operators for comparison would only clutter the language. And don't forget comparisons are not the only pitfalls when dealing with type coercion, there's also addition, subtraction, etc. So I guess they just decided to be true to their decision of allowing operations between different types. They thought that would help people when they know what they're doing, but maybe didn't anticipate all the confusion that arose from that.

like image 34
bfavaretto Avatar answered Oct 07 '22 11:10

bfavaretto