Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中比较数字时如何避免自动类型强制?_Javascript_Comparison Operators_Integer Arithmetic_Coercion_Implicit Typing - Fatal编程技术网

在Javascript中比较数字时如何避免自动类型强制?

在Javascript中比较数字时如何避免自动类型强制?,javascript,comparison-operators,integer-arithmetic,coercion,implicit-typing,Javascript,Comparison Operators,Integer Arithmetic,Coercion,Implicit Typing,我想知道在JavaScript中使用小于和大于运算符比较数字时如何避免自动强制 例如,我已经知道==运算符进行类型强制,例如: 1 == '1'; //true 1 === '1'; // false 1<2; //true 1<'2'; //true 1<'0'; //false And==运算符没有,例如: 1 == '1'; //true 1 === '1'; // false 1<2; //true 1<'2'; //true 1<'0';

我想知道在JavaScript中使用小于和大于运算符比较数字时如何避免自动强制

例如,我已经知道==运算符进行类型强制,例如:

1 == '1'; //true
1 === '1'; // false
1<2; //true
1<'2'; //true
1<'0'; //false
And==运算符没有,例如:

1 == '1'; //true
1 === '1'; // false
1<2; //true
1<'2'; //true
1<'0'; //false
但是,在比较数字时如何避免这种情况?例如:

1 == '1'; //true
1 === '1'; // false
1<2; //true
1<'2'; //true
1<'0'; //false

1通过RegExp文本使用装箱。例如:

1 == '1'; //true
1 === '1'; // false
1<2; //true
1<'2'; //true
1<'0'; //false
console.log(/3.1/>3);
console.log(/3.1/.exec(3.1)>3)

console.log((/Infinity/).toString().replace(//\//g,“””)>3
你可以做一些有趣的事,比如:
typeof a====typeof b&&a
RTTI没有乐趣,但是这个解决方案听起来足够(现在)可能是重复的