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

coldplay.xixi
coldplay.xixiOriginal
2021-04-06 17:06:295076browse

Javascript method to determine whether it is an integer: 1. Use the remainder operator to determine; 2. Use Math.round, Math.ceil, Math.floor to determine; 3. Judge through parseInt; 4. Use bit operations judge.

How to determine whether it is an integer in javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

Javascript method to determine whether it is an integer:

Method 1. Use the remainder operator to determine

Any integer will Divisible by 1, 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, Math.floor example, 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

Method 3. Determine through parseInt

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 integer to be parsed before parsing it. The first parameter is parsed into a string. 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

Related free learning recommendations: javascript(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