Home > Web Front-end > JS Tutorial > body text

How does JavaScript determine whether it is a number?

coldplay.xixi
Release: 2023-01-04 09:35:44
Original
8407 people have browsed it

Methods for JavaScript to determine whether it is a number: 1. Use the [isNaN()] function; 2. Use regular expressions; 3. Use the return value of [parseFloat()].

How does JavaScript determine whether it is a number?

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

JavaScript method to determine whether it is a number:

Method 1: Use isNaN() function

isNaN() function It is a global function that comes with js. The isNaN() function is used to check whether its parameter is a non-numeric value.

If value x is a special non-numeric value NaN (or can be converted to such a value), the returned value is true; if value x is another value, false is returned.

The disadvantage of isNaN() is that null, space and empty string will be treated as 0

NaN: Not a Number

<script>
document.write(isNaN(123));  //数字      ----false
document.write(isNaN(-1.23));  //数字    ----false
document.write(isNaN(5-2));  //数字      ----false
document.write(isNaN(0));  //数字        ----false
document.write(isNaN("Hello"));  //字符串 ----true
document.write(isNaN("2005/12/12"));  //字符串----true
</script>
Copy after login

Disadvantage: isNaN() will Null, spaces and empty strings are treated as 0, so the check is not strict.

So process it and use it with the typeof operator.

Example:

// true:数值型的,false:非数值型
function myIsNaN(value) {
   return (typeof value === &#39;number&#39; && !isNaN(value));
}
myIsNaN(10);      ----true
myIsNaN(null);    ----false
myIsNaN( );      ----false
myIsNaN();       ----false
Copy after login

Note: If it is '36.3', typeof value === 'number', it will return false. If you want string type numbers to also return true, you can use the following Method 3

Method 2: Use regular expression

(1) to check as long as it is a number (including positive and negative integers, 0 and positive and negative floating point numbers) It returns true

/**
* 校验只要是数字(包含正负整数,0以及正负浮点数)就返回true
**/
function isNumber(val){
    var regPos = /^[0-9]+.?[0-9]*/; //判断是否是数字。
  
    if(regPos.test(val) ){
        return true;
    }else{
        return false;
    }
}
Copy after login

(2). If the positive and negative numbers are verified, it returns true

/**
* 校验正负正数就返回true
**/
function isIntNum(val){
    var regPos = / ^\d+$/; // 非负整数 
    var regNeg = /^\-[1-9][0-9]*$/; // 负整数
    if(regPos.test(val) && regNeg.test(val)){
        return true;
    }else{
        return false;
    } 
}
Copy after login

Method 3: Use the return value of parseFloat()

parseFloat() function parses a string and returns a floating point number.

This function specifies whether the first character in the string is a number. If it is, the string is parsed until it reaches the end of the number, and the number is returned as a number rather than as a string.

Usage: Parse the string specified in the parameter into a floating point number and return it.

/**
* 验证数据 是数字:返回true;不是数字:返回false
**/
function Number(val) {
  if (parseFloat(val).toString() == "NaN") {
    
    return false;
  } else {
    return true;
  }
}
Copy after login

isNaN(val) cannot judge an empty string or a space

If it is an empty string, space or null, and isNaN is processed as a number 0,

And parseInt and parseFloat return an error message, which is caused by the loose isNaN check.

Related free learning recommendations: javascript video tutorial

The above is the detailed content of How does JavaScript determine whether it is a number?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template