Home >Web Front-end >JS Tutorial >How to determine whether it is an integer in javascript

How to determine whether it is an integer in javascript

青灯夜游
青灯夜游Original
2021-03-30 16:48:363642browse

Method: 1. Use the remainder operator to determine; 2. Use the "Math.round", "Math.ceil", and "Math.floor" methods to determine; 3. Use the parseInt function to determine ; 4. Judge through bit operations; 5. Use "Number.isInteger" to judge.

How to determine whether it is an integer in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Method 1. Use the remainder operator to determine

Any integer will be divisible by 1, that is, the remainder is 0. Use this rule to determine whether it is an integer.

function isInteger(obj) {
 return obj%1 === 0
}

isInteger(3) // true
isInteger(3.3) // false 

isInteger('') // true
isInteger('3') // true
isInteger(true) // true
isInteger([]) // true

Returns true for empty strings, string type numbers, Boolean true, and empty arrays. If you are interested in the internal conversion details of these types, please refer to: Weird False Values ​​in JavaScript
Therefore, you need to first determine whether the object is a number, such as adding a typeof

function isInteger(obj) {
 return typeof obj === 'number' && obj%1 === 0
}

isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

Method 2, Use Math.round, Math.ceil, and Math.floor to determine whether the

integer is still equal to itself after rounding. Use this feature to determine whether it is an integer. The Math.floor example is as follows

function isInteger(obj) {
 return Math.floor(obj) === obj
}


isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

[Recommended learning: js basic tutorial]

Method 3. Through parseInt Judgment

function isInteger(obj) {
 return parseInt(obj, 10) === obj
}

isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false、

//很不错,但也有一个缺点
isInteger(1000000000000000000000) // false

The reason is that parseInt forces the first parameter to be parsed into a string before parsing the integer. This method of converting numbers to integers is not a good choice.

Method 4. Determine through bit operations

function isInteger(obj) {
 return (obj | 0) === obj
}

isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

//这个函数很不错,效率还很高。但有个缺陷,上文提到过,位运算只能处理32位以内的数字,对于超过32位的无能为力
isInteger(Math.pow(2, 32)) // 32位以上的数字返回false了

Method 5. ES6 provides Number.isInteger

Number.isInteger(3) // true
Number.isInteger(3.1) // false
Number.isInteger('') // false
Number.isInteger('3') // false
Number.isInteger(true) // false
Number.isInteger([]) // false

More For programming related knowledge, please visit: programming video! !

The above is the detailed content of How to determine whether it is an integer in javascript. 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