We have previously recorded various ways to determine the numeric type in JavaScript. This article will look at how to determine the integer type (Integer).
JavaScript does not distinguish between integers and floating point numbers. All numbers are internally represented in a 64-bit floating point format, which is the same as Java's double type. However, actual operations such as array indexing and bit operations are based on 32-bit integers.
Method 1: Use the remainder operator to determine
Any integer is 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
The above output shows that this function is very easy to use, but it is powerless for strings and some special values
isInteger('') // true
isInteger('3') // true
isInteger(true) // true
isInteger([]) // true
True is returned for empty strings, string type numbers, Boolean true, and empty arrays, which is really unacceptable. 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
Well, this is more perfect.
2. Use Math.round, Math.ceil, Math.floor to judge
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
This directly blocks strings, true, and [], and the amount of code is less than the previous function.
3. Judge by 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
Very good, but there is a drawback
isInteger(1000000000000000000000) // false
It actually returned false, which is unreasonable. The reason is that parseInt forces the first argument to be parsed into a string before parsing the integer. This method of converting numbers to integers is not a good choice.
4. Judgment 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
This function is very good and very efficient. But there is a flaw. As mentioned above, bit operations can only handle numbers within 32 bits, and cannot do anything with numbers exceeding 32 bits, such as
isInteger(Math.pow(2, 32)) // Numbers with more than 32 digits return false
Of course, most of the time we won't use numbers that big.
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
Currently, the latest Firefox and Chrome already support it.