javascript 如何使用javascript检查var是字符串还是数字

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

how to check whether a var is string or number using javascript

javascripttypeof

提问by Romi

I have a variable var number="1234", though the number is a numeric value but it is between ""so when I check it using typeofor by NaNI got it as a string .

我有一个变量var number="1234",虽然数字是一个数值,但它介于两者之间,""所以当我使用typeof或通过NaN我将其作为字符串检查时。

function test()
{
    var number="1234"

if(typeof(number)=="string")
{
    alert("string");
}
if(typeof(number)=="number")
{
    alert("number");
}

}

I got always alert("string"), can you please tell me how can I check if this is a number?

我总是得到alert("string"),你能告诉我如何检查这是否是一个数字吗?

采纳答案by Facebook Staff are Complicit

The line

线

 var number = "1234";

creates a new String object with the value "1234". By putting the value in quotes, you are saying that it a string.

创建一个值为“1234”的新字符串对象。通过将值放在引号中,您就是说它是一个字符串。

If you want to check if a string only contains numeric digits, you can use regular expressions.

如果要检查字符串是否仅包含数字,可以使用正则表达式

if (number.match(/^-?\d+$/)) {
    alert("It's a whole number!");
} else if (number.match(/^-?\d+*\.\d+$/)) {
    alert("It's a decimal number!");
}

The pattern /^\d+$/means: at the start (^) of the string, there is an optional minus sign (-?), then a digit (\d), followed by any more digits (+), and then the end of the string ($). The other pattern just looks for a point between the groups of digits.

该模式/^\d+$/表示:在^字符串的开头 ( ),有一个可选的减号 ( -?),然后是一个数字 ( \d),然后是更多的数字 ( +),然后是字符串的结尾 ( $)。另一个模式只是寻找数字组之间的一个点。

回答by Eineki

As far I understand your question you are asking for a test to detect if a string represents a numeric value.

据我了解您的问题,您要求进行测试以检测字符串是否表示数值。

A quick test should be

快速测试应该是

function test() {
   var number="1234"
   return (number==Number(number))?"number":"string"
}

As Number, if called without the newkeyword convert the string into a number. If the variable content is untouched (==will cast back the numeric value to a string) you are dealing with a number. It is a string otherwise.

作为数字,如果在没有new关键字的情况下调用,则将字符串转换为数字。如果变量内容未受影响(==将数值转换为字符串),则您正在处理一个数字。否则它是一个字符串。

function isNumeric(value) {
   return (value==Number(value))?"number":"string"
}

/* tests evaluating true */
console.log(isNumeric("1234"));  //integer
console.log(isNumeric("1.234")); // float
console.log(isNumeric("12.34e+1")); // scientific notation
console.log(isNumeric(12));     // Integer
console.log(isNumeric(12.7));   // Float
console.log(isNumeric("0x12")); // hex number

/* tests evaluating false */
console.log(isNumeric("1234e"));
console.log(isNumeric("1,234"));
console.log(isNumeric("12.34b+1"));
console.log(isNumeric("x"));

回答by Ibu

because var number="1234"is a string. the double quotes makes it a literal.

因为var number="1234"是一个字符串。双引号使它成为文字。

if you want a number use it like this

如果你想要一个数字,像这样使用它

var number = 1234;

Update:

更新:

If you are taking the input from a input tag forexample, the dataType will be string, if you want to convert it to a number, you can use the parseInt() function

例如,如果您从 input 标签中获取输入,则 dataType 将是字符串,如果您想将其转换为数字,则可以使用 parseInt() 函数

var number = "1234";

var newNumber = parseInt(number);

alert(typeof newNumber); // will result in string

回答by Quentin

Convert it to a number then compare it to the original string.

将其转换为数字,然后将其与原始字符串进行比较。

if ( parseFloat(the_string,10) == the_string ) {
    // It is a string containing a number (and only a number)
}

回答by Felix Kling

Another easy way:

另一个简单的方法:

var num_value = +value;
if(value !== '' && !isNaN(num_value)) {
    // the string contains (is) a number
}