Combined Comparison / “Spaceship” Operator (<=>) in Javascript?

后端 未结 2 1768
[愿得一人]
[愿得一人] 2021-02-07 00:11

Ruby has something called a Combined Comparison or \"Spaceship\" Operator, it looks like this: <=>

It does the following:

a <=>          


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 00:30

    As far as I know there is no such operator in JavaScript but you can use Math.sign() function:

    Math.sign(a - b);
    

    NOTE: As was mentioned in comments, Math.sign() is not currently supported by all browsers. Check for compatibility (MDN).

提交回复
热议问题