Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading