JS method to determine whether it is a number: 1. Use the isNaN() function to determine whether it is a number; 2. Use regular expressions to determine whether it is a number; 3. Use the return value of parseFloat() to realize the judgment.
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
Method 1: Use the isNaN() function
The isNaN() function is a global function that comes with js. The isNaN() function is used to check whether its parameter is a non-number. 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)); //数字 document.write(isNaN(-1.23)); //数字 document.write(isNaN(5-2)); //数字 document.write(isNaN(0)); //数字 document.write(isNaN("Hello")); //字符串 document.write(isNaN("2005/12/12")); //字符串 </script>
Output:
false false false false true true
Disadvantage: isNaN() will treat 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) { document.write((typeof value === 'number' && !isNaN(value))+"<br>"); } myIsNaN(10); myIsNaN(null); myIsNaN( ); myIsNaN();
Output:
true false false false
Method 2: Use regular expression
(1), check as long as If it is a number (including positive and negative integers, 0 and positive and negative floating point numbers), it will return true
/** * 校验只要是数字(包含正负整数,0以及正负浮点数)就返回true **/ function isNumber(val){ var regPos = /^[0-9]+.?[0-9]*/; //判断是否是数字。 if(regPos.test(val) ){ return true; }else{ return false; } }
(2). If it is a positive and negative number, it will return 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; } }
Method 3 : Use the return value of parseFloat()
parseFloat() function to parse a string and return 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; } } //isNaN(val)不能判断空串或一个空格 //如果是一个空串、空格或null,而isNaN是做为数字0进行处理的, //而parseInt与parseFloat是返回一个错误消息,这个isNaN检查不严密而导致的。
Recommended study: "js Basic Tutorial"
The above is the detailed content of How to determine whether it is a number in js. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools