javascript 中的类型检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4514602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:44:50  来源:igfitidea点击:

type checking in javascript

javascripttypes

提问by sova

How can I check if a variable is currently an integer type? I've looked for some sort of resource for this and I think the === operator is important, but I'm not sure how to check if a variable is an Integer (or an Array for that matter)

如何检查变量当前是否为整数类型?我为此寻找了某种资源,我认为 === 运算符很重要,但我不确定如何检查变量是否为整数(或与此相关的数组)

回答by Quentin

A variable will never be an integer type in JavaScript — it doesn't distinguish between different types of Number.

JavaScript 中的变量永远不会是整数类型——它不区分不同类型的 Number。

You can test if the variable contains a number, and if that number is an integer.

您可以测试变量是否包含数字,以及该数字是否为整数。

(typeof foo === "number") && Math.floor(foo) === foo

If the variable might be a string containing an integer and you want to see if that is the case:

如果变量可能是一个包含整数的字符串,并且您想查看是否是这种情况:

foo == parseInt(foo, 10)

回答by dat

These days, ECMAScript 6 (ECMA-262) is "in the house". Use Number.isInteger(x)to ask the question you want to ask with respect to the type of x:

现在,ECMAScript 6 (ECMA-262) 已经“在家里”了。用Number.isInteger(x)问你要问关于x的类型的问题:

js> var x = 3
js> Number.isInteger(x)
true
js> var y = 3.1
js> Number.isInteger(y)
false

回答by kennebec

A number is an integer if its modulo %1 is 0-

如果一个数字的模 %1 是 0-,则该数字是一个整数

function isInt(n){
    return (typeof n== 'number' && n%1== 0);
}

This is only as good as javascript gets- say +- ten to the 15th.

这仅与 javascript 一样好 - 比如说 +- 10 到 15 日。

isInt(Math.pow(2,50)+.1)returns true, as does Math.pow(2,50)+.1 == Math.pow(2,50)

isInt(Math.pow(2,50)+.1)返回true,就像 Math.pow(2,50)+.1 == Math.pow(2,50)

回答by Dan

I know you're interested in Integer numbers so I won't re answer that but if you ever wanted to check for Floating Point numbers you could do this.

我知道你对整数感兴趣,所以我不会再回答这个问题,但如果你想检查浮点数,你可以这样做。

function isFloat( x )
{
    return ( typeof x === "number" && Math.abs( x % 1 ) > 0);
}

Note: This MAY treat numbers ending in .0(or any logically equivalent number of 0's) as an INTEGER. It actually needs a floating point precision error to occur to detect the floating point values in that case.

注意:这可以将以.0(或任何逻辑上等效的0's数)结尾的数字视为整数。在这种情况下,它实际上需要发生浮点精度错误来检测浮点值。

Ex.

前任。

alert(isFloat(5.2));   //returns true
alert(isFloat(5));     //returns false
alert(isFloat(5.0));   //return could be either true or false

回答by eSniff

Try this code:

试试这个代码:

 alert(typeof(1) == "number");

回答by Chris West

Quite a few utility libraries such as YourJSoffer functions for determining if something is an array or if something is an integer or a lot of other types as well. YourJS defines isIntby checking if the value is a number and then if it is divisible by 1:

相当多的实用程序库(例如YourJS)提供了用于确定某物是数组还是整数或许多其他类型的函数。YourJS通过检查值是否为数字以及是否可以被 1 整除来定义isInt

function isInt(x) {
  return typeOf(x, 'Number') && x % 1 == 0;
}

The above snippet was taken from this YourJS snippetand thusly only works because typeOfis defined by the library. You can download a minimalistic version of YourJS which mainly only has type checking functions such as typeOf(), isInt()and isArray(): http://yourjs.com/snippets/build/34,2

上面的代码片段取自这个 YourJS 代码片段,因此只能工作,因为它typeOf是由库定义的。你可以下载一个简约版的YourJS,它主要只有类型检查功能,例如typeOf()isInt()isArray():http: //yourjs.com/snippets/build/34,2

回答by vitalets

You may also have a look on Runtyper- a tool that performs type checking of operands in ===(and other operations).
For your example, if you have strict comparison x === yand x = 123, y = "123", it will automatically check typeof x, typeof yand show warning in console:

您还可以查看Runtyper- 一种对操作数===(和其他操作)执行类型检查的工具。
对于您的示例,如果您有严格的比较x === yand x = 123, y = "123",它将自动检查typeof x, typeof y并在控制台中显示警告:

Strict compare of different types: 123 (number) === "123" (string)

不同类型的严格比较:123(数字)===“123”(字符串)