I have recorded many ways to judge a number type in JavaScript. This article shows how to determine an integer type (Integer).
In JavaScript, integers and floating-point numbers are not distinguished. All numbers are represented in 64-bit floating-point format, the same as Java's double type. But in actual operations, such as array indexing and bit operations, are based on 32-bit integers.
Method 1: Use the remaining operator to judge
Any integer will be divisible by 1, that is, the remainder is 0. Use this rule to determine whether it is an integer.
The code copy is as follows:
function isInteger(obj) {
return obj%1 === 0
}
isInteger(3) // true
isInteger(3.3) // false
The above output shows that this function is very useful, but it seems to be incompetent for strings and some special values.
The code copy is as follows:
isInteger('') // true
isInteger('3') // true
isInteger(true) // true
isInteger([]) // true
It is really hard to accept that empty strings, string type numbers, boolean true, and empty arrays all return true. If you are interested in the details of these types of internal conversion, please refer to: Weird fake values in JavaScript
Therefore, you need to first determine whether the object is a number, such as adding a typeof
The code copy is as follows:
function isInteger(obj) {
return typeof obj === 'number' && obj%1 === 0
}
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false
Well, this is more perfect.
2. Use Math.round, Math.ceil, and Math.floor to make judgments
The integer is still equal to yourself. Use this feature to determine whether it is an integer. The Math.floor example is as follows
The code copy 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
This directly blocks the string, true, [], and the amount of code is smaller than the previous function.
3. Judgment through parseInt
The code copy is as follows:
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
Very good, but also has one disadvantage
The code copy is as follows:
isInteger(10000000000000000000000) // false
It actually returned false, it was unreasonable. The reason is that parseInt forces the first parameter to be parsed into a string before parsing the integer. This method of converting numbers into integers is not a good choice.
4. Judgment through bit operations
The code copy is as follows:
function isInteger(obj) {
return (obj | 0) === obj
}
isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false
This function is very good and has high efficiency. But there is a flaw. As mentioned above, bit operations can only process numbers within 32 bits, and they are powerless to do anything more than 32 bits, such as
The code copy is as follows:
isInteger(Math.pow(2, 32)) // Numbers above 32 digits have returned false
Of course, most of the time we don’t use such a large number.
5. ES6 provides Number.isInteger
The code copy is as follows:
Number.isInteger(3) // true
Number.isInteger(3.1) // false
Number.isInteger('') // false
Number.isInteger('3') // false
Number.isInteger(true) // false
Number.isInteger([]) // false
Currently, the latest Firefox and Chrome are already supported.