admin管理员组文章数量:1205165
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
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
Share Improve this question edited Oct 26, 2012 at 17:03 kanaka asked Oct 26, 2012 at 16:46 kanakakanaka 73.1k23 gold badges147 silver badges143 bronze badges 12 | Show 7 more comments2 Answers
Reset to default 14There 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
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)) ;
本文标签:
版权声明:本文标题:Best andor shortest way to do strict (non-type converting) <, >, <=, >= comparison in Javascript - S 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738681495a2106579.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
[] < 1
, or{} >= []
? – bfavaretto Commented Oct 26, 2012 at 16:49"2" >== 1
should return false – Ruan Mendes Commented Oct 26, 2012 at 16:50"1" < 2
. I use strict cases to prevent stupid logic errors in my code, and I assume that is what he is doing as well. – thatidiotguy Commented Oct 26, 2012 at 16:51sweet.js
– clentfort Commented Oct 26, 2012 at 16:59