node.js 如何检查节点 js 中的数据类型 - 特别是整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19048507/
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-09-02 15:46:33  来源:igfitidea点击:

how to check for datatype in node js- specifically for integer

javascriptnode.js

提问by Prem

I tried the following to check for the datatype (specifically for integer), but not working.

我尝试了以下方法来检查数据类型(特别是整数),但不起作用。

var i = "5";

if(Number(i) = 'NaN')
{
 console.log('This is not number'));
}

回答by Endre Simo

I think of two ways to test for the type of a value:

我想到了两种测试值类型的方法:

Method 1:

方法一:

You can use the isNaNjavascript method, which determines if a value is NaNor not. But because in your case you are testing a numerical value converted to string, Javascript is trying to guess the type of the value and converts it to the number 5 which is not NaN. That's why if you console.logout the result, you will be surprised that the code:

您可以使用isNaNjavascript 方法,该方法确定值是否为NaN。但是因为在您的情况下您正在测试转换为字符串的数值,Javascript 试图猜测该值的类型并将其转换为数字 5 而不是NaN. 这就是为什么如果你console.log输出结果,你会惊讶于代码:

if (isNaN(i)) {
    console.log('This is not number');
}

will not return anything. For this reason a better alternative would be the method 2.

不会返回任何东西。因此,更好的替代方法是方法 2。

Method 2:

方法二:

You may use javascript typeofmethod to test the type of a variable or value

您可以使用 javascript typeof方法来测试变量或值的类型

if (typeof i != "number") {
    console.log('This is not number');
}

Notice that i'm using double equal operator, because in this case the type of the value is a string but Javascript internally will convert to Number.

请注意,我使用的是双相等运算符,因为在这种情况下,值的类型是字符串,但 Javascript 内部将转换为数字。

A more robust method to force the value to numerical type is to use Number.isNaNwhich is part of new Ecmascript 6 (Harmony) proposal, hence not widespread and fully supported by different vendors.

将值强制为数字类型的更健壮的方法是使用Number.isNaN,它是新的 Ecmascript 6 (Harmony) 提案的一部分,因此没有广泛使用,也没有得到不同供应商的完全支持。

回答by Mayur Gupta

i have used it in this way and its working fine

我以这种方式使用它并且它工作正常

quantity=prompt("Please enter the quantity","1");
quantity=parseInt(quantity);
if (!isNaN( quantity ))
{
    totalAmount=itemPrice*quantity;

}
return totalAmount;

回答by Gustav Kusnir

I just made some tests in node.js v4.2.4 (but this is true in any javascript implementation):

我刚刚在 node.js v4.2.4 中做了一些测试(但在任何 javascript 实现中都是如此):

> typeof NaN
'number'
> isNaN(NaN)
true
> isNaN("hello")
true

the surprise is the first one as type of NaN is "number", but that is how it is defined in javascript.

令人惊讶的是第一个,因为 NaN 的类型是“数字”,但这就是它在 javascript 中的定义方式。

So the next test brings up unexpected result

所以下一个测试带来了意想不到的结果

> typeof Number("hello")
"number"

because Number("hello") is NaN

因为 Number("hello") 是 NaN

The following function makes results as expected:

以下函数使结果符合预期:

function isNumeric(n){
  return (typeof n == "number" && !isNaN(n));
}

回答by FKasa

Your logic is correct but you have 2 mistakes apparently everyone missed:

您的逻辑是正确的,但显然每个人都错过了 2 个错误:

just change if(Number(i) = 'NaN')to if(Number(i) == NaN)

只需更改if(Number(i) = 'NaN')if(Number(i) == NaN)

NaNis a constant and you should use doubleequality signs to compare, a single one is used to assign values to variables.

NaN是一个常数,您应该使用等号进行比较,一个等号用于为变量赋值。

回答by chaitanya

you can try this one isNaN(Number(x))where x is any thing like string or number

你可以试试这个isNaN(Number(x)),其中 x 是字符串或数字之类的任何东西

回答by Alain Beauvois

If you want to know if "1" ou 1 can be casted to a number, you can use this code :

如果您想知道是否可以将“1”或 1 转换为数字,可以使用以下代码:

if (isNaN(i*1)) {
     console.log('i is not a number'); 
}

回答by Jacob Avners

var val = ... //the value you want to check
if(Number.isNaN(Number(val))){
  console.log("This is NOT a number!");
}else{
  console.log("This IS a number!");
}

回答by ?smail Ceylan

You can check your numbers by checking their constructor.

您可以通过检查它们的构造函数来检查您的数字。

var i = "5";

if( i.constructor !== Number )
{
 console.log('This is not number'));
}