Summary of multiple methods for determining integers in JavaScript, javascript Integers
I have previously recorded multiple methods for determining the digit type in JavaScript. This article describes how to determine the Integer type ).
JavaScript does not distinguish between integers and floating-point numbers. All numbers are expressed in 64-bit floating-point format, which is the same as Java's double type. However, in actual operations, for example, array indexes 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.
Copy codeThe Code is as follows:
Function isInteger (obj ){
Return obj % 1 = 0
}
IsInteger (3) // true
Isintegers (3.3) // false
The above output shows that this function is quite useful, but it cannot be used for strings or some special values.
Copy codeThe Code is as follows:
IsInteger ('') // true
IsInteger ('3') // true
IsInteger (true) // true
IsInteger ([]) // true
True is returned for null strings, string-type numbers, Boolean true, and empty arrays, which is really unacceptable. For details about the internal conversion of these types, see the odd false values in JavaScript.
Therefore, you must first determine whether the object is a number, such as adding a typeof
Copy codeThe Code 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 perfect.
2. Use Math. round, Math. ceil, and Math. floor to Judge
The integer is equal to itself after being rounded. This feature is used to determine whether it is an integer. The Math. floor example is as follows:
Copy codeThe Code is as follows:
Function isInteger (obj ){
Return Math. floor (obj) === obj
}
IsInteger (3) // true
Isintegers (3.3) // false
IsInteger ('') // false
IsInteger ('3') // false
IsInteger (true) // false
IsInteger ([]) // false
This directly blocks the string, true, [], with less code than the previous function.
Iii. parseInt-based judgment
Copy codeThe Code is as follows:
Function isInteger (obj ){
Return parseInt (obj, 10) === obj
}
IsInteger (3) // true
Isintegers (3.3) // false
IsInteger ('') // false
IsInteger ('3') // false
IsInteger (true) // false
IsInteger ([]) // false
Very good, but there is also a drawback
Copy codeThe Code is as follows:
Isintegers (1000000000000000000000) // false
It turns out that false is returned. It's not reasonable. The reason is that parseInt forces the first parameter to be parsed into a string before the integer is parsed. This method is not a good choice to convert a number into an integer.
4. bitwise operation
Copy codeThe Code is as follows:
Function isInteger (obj ){
Return (obj | 0) = obj
}
IsInteger (3) // true
Isintegers (3.3) // false
IsInteger ('') // false
IsInteger ('3') // false
IsInteger (true) // false
IsInteger ([]) // false
This function is very good and efficient. However, there is a flaw. As mentioned above, bitwise operations can only process numbers within 32 bits, and there is nothing to do with the number exceeding 32 bits, as shown in figure
Copy codeThe Code is as follows:
IsInteger (Math. pow (2, 32) // if the number is greater than 32 bits, false is returned.
Of course, most of the time we don't use that big number.
5. ES6 provides Number. isInteger
Copy codeThe Code is as follows:
Number. isInteger (3) // true
No. 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 supported.