Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best and/or shortest way to do strict (non-type converting) <, >, <=, >= comparison in Javascript

Tags:

javascript

In Javascript, the == comparison has a strict (non-type converting) version: ===. Likewise, != has the strict form !==. These protect you from the following craziness:

var s1 = "1",     i1 = 1,     i2 = 2;  (s1 == i1)   // true, type conversion (s1 != i1)   // false, type conversion  (s1 === i1)  // false, no type conversion (s1 !== i1)  // true, no type conversion 

However, the other comparison operators have no equivalent strict modes:

(s1 < i2)   // true, type conversion (s1 <= i2)  // true, type conversion ([] < i2)   // true, wait ... wat!? 

The obvious solution seems pretty verbose:

((typeof s1 === typeof i2) && (s1 < i2))  // false 

Is there a more idiomatic (or just less verbose) way to do this in Javascript?

Reference: MDN Comparison Operators

like image 614
kanaka Avatar asked Oct 26 '12 16:10

kanaka


People also ask

What does <= mean in JavaScript?

The less than or equal operator ( <= ) returns true if the left operand is less than or equal to the right operand, and false otherwise.

Which is faster == or === in JavaScript?

So === faster than == in Javascript === compares if the values and the types are the same. == compares if the values are the same, but it also does type conversions in the comparison. Those type conversions make == slower than ===.

Why do we prefer === and !== Over == and != In JavaScript?

The strict equality operator ( === ) behaves identically to the abstract equality operator ( == ) except no type conversion is done, and the types must be the same to be considered equal. The == operator will compare for equality after doing any necessary type conversions.

What's the difference between == and === comparison operators 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.


2 Answers

There are no built-in operators for what you want, but you can always create your own functions. For example, for <:

function lt(o1, o2) {     return ((typeof o1 === typeof o2) && (o1 < o2)); } lt("10", 11); // false 

Another option, if you're only dealing with strings and numbers, is extending String.prototype and Number.prototype:

function lt(o) {     return ((typeof this.valueOf() === typeof o) && (this < o)); } String.prototype.lt = lt; Number.prototype.lt = lt; "10".lt(11);   // false (11).lt("12"); // false 
like image 172
bfavaretto Avatar answered Oct 06 '22 22:10

bfavaretto


How about creating a Object and using it

var strictComparison = {     "<" : function(a,b) { return ((typeof a === typeof b) && (a < b)) },     "<=" : function(a,b) { return ((typeof a === typeof b) && (a <= b)) },     ">" : function(a,b) { return ((typeof a === typeof b) && (a > b)) },     ">=" : function(a,b) { return ((typeof a === typeof b) && (a >= b)) } };  console.log(strictComparison["<"](5,"6")) ;   console.log(strictComparison[">"](5,6)) ;    
like image 29
Sushanth -- Avatar answered Oct 06 '22 20:10

Sushanth --