Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript equality triple equals but what about greater than and less than?

Tags:

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)?

like image 979
kmp Avatar asked Aug 27 '12 14:08

kmp


People also ask

Is == and === same in JavaScript?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

What is triple === in JavaScript?

This means that before checking the values, it converts the types of the variables to match each other. On the other hand, Triple Equals ( === ) does not perform type coercion. It will verify whether the variables being compared have both the same value AND the same type.

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 does the === mean in JavaScript?

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.


2 Answers

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.

like image 194
Pointy Avatar answered Oct 28 '22 00:10

Pointy


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 
like image 28
Felix Kling Avatar answered Oct 28 '22 00:10

Felix Kling