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

后端 未结 4 2019
轮回少年
轮回少年 2021-02-07 05:11

I was explaining to a colleague that you should use === and !== (and >== and <== of course) when comparing variables in

相关标签:
4条回答
  • 2021-02-07 05:39
    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).

    0 讨论(0)
  • 2021-02-07 05:43

    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
    
    0 讨论(0)
  • 2021-02-07 05:48

    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.

    0 讨论(0)
  • 2021-02-07 05:52

    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) { ... }
    
    0 讨论(0)
提交回复
热议问题