javascriptcomparisonoperationweak

In Javascript, is there a way to make strict comparison "greater than" or "smaller than" operations?


Is there a way in Javascript to make strict comparison operations other than the one allowed with '===', i.e., a strict '<', '>', '<=' and '>='?

The code below using '<' performs a weak operation between a string and an integer. I would like to know if this could be achieved as done with '==='.

let a = '9';
let b = 10;
if (a < b) {
    console.log('Success');
}  // Prints 'Success'

Thanks!


Solution

  • No, there is no such set of operators. Also they would violate the usual expectation that (a < b) != (a >= b) / (a <= b) != (a > b)1.

    Of course you can build this yourself by writing

    if (typeof a == typeof b && a < b) …
    

    1: this expectation doesn't actually hold for the normal operators either, there are values like NaN that violate it.