How does JavaScript determine whether it is a number?
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()].
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>
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 === 'number' && !isNaN(value)); } myIsNaN(10); ----true myIsNaN(null); ----false myIsNaN( ); ----false myIsNaN(); ----false
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; } }
(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; } }
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; } }
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Real-time collaborative editors have become a standard feature of modern web development, especially in various team collaboration, online document editing and task management scenarios. Real-time communication technology based on WebSocket can improve communication efficiency and collaboration effects among team members. This article will introduce how to use WebSocket and JavaScript to build a simple online collaborative editor to help readers better understand the principles and usage of WebSocket. Understand the basic principles of WebSocketWebSo

On May 7, our mobile phone manufacturer officially announced that our company’s GTNeo6 launch conference is scheduled for May 9. GTNoe6 is positioned as a "performance storm", aiming to stir up the mid-range machine situation. In addition, this conference will also be the first AI digital human conference in the mobile phone industry. At that time, Realme Vice President, Global Marketing President, and China President Xu Qi will appear at the press conference in the form of a digital human. Digital man Xu Qi According to the official introduction, Realme GTNoe6, codenamed "Hurricane", is faster and stronger. It will challenge the strongest third-generation Snapdragon 8s flagship and the strongest product in its class. Recently, the Realme GTNeo6 was found to be directly on the e-commerce platform. Some core configurations were exposed, showing that the machine is not only equipped with a Snapdragon 8s processor, but also supports 120W flash charging.
