Home > Web Front-end > JS Tutorial > body text

How to determine whether it is an integer in javascript

青灯夜游
Release: 2023-01-03 09:31:52
Original
3586 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

[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
Copy after login

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了
Copy after login

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
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!