21

I was explaining to a colleague that you should use === and !== (and >== and <== of course) when comparing variables in JavaScript so that it doesn't coerce the arguments and get all froopy and confusing but they asked me a two part question that I did not know the answer to and thought I would ask the experts here, specifically it is:

What about > and < - when they compare do they also coerce the arguments or not - why isn't there some sort of >> and << operator (probably need to be some other syntax as I would guess they would be bit shift operators if it is going along the whole C style but you get the gist)?

So I can write a test to find the answer to the first part, which I did, here it is:

// Demo the difference between == and ===
alert(5 == "5");
alert(5 === "5");   

// Check out what happens with >
alert(5 > "4");    
alert(5 > 4);

and it returned:

true
false

true
true

so it does look like the > is doing the coercion since > "4" and > 4 return the same result. so how about the second part...

Is there some sort of operator for > and < that do not coerce the type (or how can I change my test to perform the test safely)?

kmp
  • 10,535
  • 11
  • 75
  • 125
  • 4
    There actually isn't a `>==` or `<==` in javascript for the same reason the answers described. See [comparison operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators) – None Apr 02 '15 at 17:42

4 Answers4

19

No, there's no need for such operators. The type checking done for those relational operators is different than for equality and inequality. (edit — perhaps it's a little strong to say that there's "no need"; that's true only because JavaScript deems it so :-)

Specifically, the > and < and >= and <= operators all operate either on two numeric values, or two strings, preferring numeric values. That is, if one value is a number, then the other is treated as a number. If a non-number can't be cleanly converted to a number (that is, if it ends up as NaN), then the result of the comparison is undefined. (That's a little problematic, because undefined will look like false in the context of an if statement.)

If both values are strings, then a collating-order string comparison is performed instead.

If you think about it, these comparisons don't make any sense for object instances; what does it mean for an object to be "greater than" another? I suppose, perhaps, that this means that if you're finding yourself with values of variant types being compared like this, and that's causing problems, then yes you have to detect the situation yourself. It seems to me that it would be good to work upstream and think about whether there's something fishy about the code that's leading to such a situation.

Pointy
  • 405,095
  • 59
  • 585
  • 614
11

Is there some sort of operator for > and < that do not coerce the type

No.

how can I change my test to perform the test safely

You would have to explicitly test the types:

typeof a === typeof b && a > b
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

I referenced Flanagan's JavaScript: The Definitive Guide (5th Ed.) and there does not seem to be non-coercive comparison operators.

You are right in saying the << and >> are indeed bitwise operators so that wouldn't work.

I would suggest you deliberately coerce the values youself:

var num_as_string = '4';
var num = +num_as_string;
if (5 > num) { ... }
chelmerich
  • 193
  • 1
  • 11
  • What does the `+` syntax mean in `var num = +num_as_string;`? – Yatharth Agarwal Oct 13 '12 at 10:24
  • 1
    @YatharthROCK: It's the unary plus operator and converts a value to a number: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Arithmetic_Operators#-_.28Unary_Negation.29 – Felix Kling Oct 13 '12 at 10:29
1
12 > '100' // false
'12' > 100 // false
'12' > '100' // true

As others mentioned, if one is a number the other is casted to a number. Same rule applies to these cases as well:

null > 0 // false
null < 0 // false
null >= 0 // true

However, there might be cases that you would need null >= 0 to give false (or any of the number string comparison cases above), therefore it is indeed a need to have strict comparison >== or <==.

For example, I am writing a compareFunction for the Array.prototype.sort() and an expression like x>=0 would treat null values like 0's and put them together, whereas I want to put them elsewhere. I have to write extra logic for those cases.

Javascript says deal with it on your own (in practice).

mehmet
  • 7,720
  • 5
  • 42
  • 48