Home > Web Front-end > JS Tutorial > How to determine whether it is a number in es6

How to determine whether it is a number in es6

青灯夜游
Release: 2022-03-08 17:25:27
Original
3317 people have browsed it

In es6, you can use the isFinite() method of the Number object to determine whether the value is a number. This method can detect whether the parameter value passed in is a finite number; the syntax "Number.isFinite(value) ".

How to determine whether it is a number in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

ES6 provides us with a method for judging numbers. Please see the following code for details

Number.isFinite Judging numbers

## The #Number.isFinite() method is used to detect whether the passed parameter is a finite number.

let a = 1
console.log(Number.isFinite(a));  // true
console.log(Number.isFinite("beline"));  //false
console.log(Number.isFinite(NaN));  // false  
console.log(Number.isFinite(undefined));  // false
Copy after login

Number.isNaN determines whether it is a non-number

console.log(Number.isNaN(NaN));  // true
console.log(Number.isNaN(1));  // false
Copy after login

Number.isInteger determines whether it is an integer

let a = 66
console.log(Number.isInteger(a));   // true
Copy after login

If you need to determine whether it is a floating point type, just add the inverse sign in front of the object

let a = 111.77
console.log(!Number.isInteger(a));   // true
Copy after login

Safe integer

The safe value range of computer numeric types is 2 to the 53rd power.

let num = Math.pow(2, 53) - 1;
console.log(num ) // 9007199254740991
Copy after login

Why does ES6 provide constants for the maximum safe integer and the minimum safe integer? You can also judge the incoming value through the isSafeInteger method. Whether the value is within the safe integer range. In daily work, if it exceeds this number, we need to convert the value into a string and display it to the user

console.log(Number.MAX_SAFE_INTEGER)  // 9007199254740991
console.log(Number.MIN_SAFE_INTEGER)  // -9007199254740991
// 判断num是否在安全整数范围内
console.log(Number.isSafeInteger(num))  // true
Copy after login

[Related recommendations:

javascript video tutorialweb front end

The above is the detailed content of How to determine whether it is a number in es6. 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