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

How to determine whether it is a number in es6

青灯夜游
青灯夜游Original
2022-03-08 17:24:363284browse

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

Number.isNaN determines whether it is a non-number

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

Number.isInteger determines whether it is an integer

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

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

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

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

[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!

Statement:
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