Get the App
SLTechnology News&Howtos  ›  Development  › 

How does javascript determine whether a specified value is a number or not

Shulou Source: shulou.com Published: 2022-06-03 05:20:44 10月25日 Update

Editor to share with you javascript how to judge whether the specified value is a number, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Judgment method: 1, the use of isNaN () function, syntax "isNaN (value)", if the value is a number, then return false;2, using the return value of typeof, syntax "typeof (value)", if the return value is "Number", it is a number; 3, use regular expression to judge.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript determines whether the specified value is a number.

Method 1:isNaN () function

The isNaN () function determines whether the value is nonnumeric (Not-a-Number). If the value is equal to NaN, this function returns true. Otherwise, false is returned.

Var c = "hello"; / / string isNaN (c); / / returns a true;var cantilever 10; / / numeric isNaN (c); / / returns a false

The disadvantage of isNaN () is that null, spaces, and empty strings are treated as zeros and need to be optimized.

/ * determine whether it is a number * * / function isRealNum (val) {/ / isNaN () function processes empty string spaces and NUll as 0, so remove first, if (val = = "" | val = = null) {return false } if (! isNaN (val)) {/ / for an empty array and an array with only one numeric member or a string full of numbers, / / isNaN returns false, for example: '123', [], [2], ['123'], isNaN returns false, / / so if you don't need val to include these special cases Then the judgment is rewritten to if (! isNaN (val) & & typeof val = = 'number') return true } else {return false;}}

Method 2: use the return value of typeof

Verification method: if the returned value is Number, it is a number; if the return value is String or other, it is not a number. As follows:

Var / Numbertypeof (b) / / String

Method 3: use regular expressions

(1) true is returned as long as the check is a number (including positive and negative integers, 0 and positive or negative floating point numbers)

/ * check returns true**/function isNumber (val) {var regPos = / ^\ d + (\.\ d +) as long as it is a number (including positive and negative integers, 0 and positive or negative floating point numbers)? $/ / / Nonnegative floating point number var regNeg = / ^ (- (([0-9] +\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $/ / / negative floating point if (regPos.test (val) & & regNeg.test (val)) {return true;} else {return false;}}

(2) check positive or negative numbers and return true

/ * * check a positive or negative number and return true**/function isIntNum (val) {var regPos = / ^\ dprinter; / / non-negative integer var regNeg = / ^\-[1-9] [0-9] * $/; / negative integer if (regPos.test (val) & regNeg.test (val)) {return true;} else {return false }} above is all the content of the article "how to determine whether the specified value is a number or not by javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Numbers positive and negative functions methods integers points articles content characters strings arrays regular positive numbers spaces expressions syntax processing special not much only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS OPPO Reno Shulou Information NVidia