Whether the five JS judgments are integer type
Snandy
This article mainly introduces five kinds of JavaScript to determine whether it is an integer type, the need for friends can refer to the following
This article looks at how to determine the integer type (integer), which does not distinguish between integers and floating-point numbers in JavaScript, and all digits are represented in a 64-bit floating-point format, just like Java's double type. But the actual operation, such as array index, bit operation is based on 32-bit integers.
Method one, using the take-up operator to judge
Any integer will be divisible by 1, i.e. the remainder is 0. Use this rule to determine if it is an integer.
?
12345 |
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 for strings and some special values appear to be inadequate
?
1234 |
isInteger( ‘‘ ) // true isInteger( ‘3‘ ) // true isInteger( true ) // true isInteger([]) // true |
It is unacceptable for empty strings, String type numbers, Boolean true, and empty arrays to return true. For more information on these types of internal conversion details, please refer to: False values of wonderful flowers in JavaScript
Therefore, you need to first determine whether the object is a number, such as adding a typeof
?
1234567 |
function isinteger (obj) {   return Code class= "JS keyword" >typeof obj = = && obj%1 = = 0 } isinteger ( " //false isinteger ( ' 3 ' ) //false isinteger ( true //false isinteger ([]) //false |
Well, it's more perfect.
Method Two, use Math.Round, Math.ceil, Math.floor judgment
Integers are equal to themselves after rounding. Use this feature to determine if it is an integer, math.floor example, as follows
?
123456789 |
function isinteger (obj) {   return math.floor (obj) = = = obj } isinteger (3) / /true isinteger (3.3) //false isinteger ( " //false isinteger ( ' 3 ' ) //false isinteger ( true Code class= "JS plain" > //false isinteger ([]) //false |
This directly blocks the string, true,[], and has less code than the previous function.
Method Three, judge by parseint
?
123456789 |
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 disadvantage
?
1 |
isInteger(1000000000000000000000) // false |
Unexpectedly return false, no justice AH. The reason is that parseint forces the first argument to be parsed into a string before parsing the integer. This method is not a good choice for converting numbers into integers.
Method Four, judge by the bit operation
?
123456789 |
function isinteger (obj) {   return (obj | 0) = = = Obj } isinteger (3) // True isinteger (3.3) //false isinteger ( " //false isinteger ( ' 3 ' Code class= "JS Plain" >) //false isinteger ( Code class= "JS keyword" >true //false isinteger ([]) //false |
This function is very good, the efficiency is very high. But there is a flaw, as mentioned above, that bit arithmetic can only handle numbers within 32 digits, for more than 32 bits of powerless, such as
Copy CodeThe code is as follows: Isinteger (Math.pow (2, 32))//32 digits above returns false
Of course, most of the time we don't use that big number.
Way Five, ES6 provided the Number.isinteger
?
123456 |
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 have been supported.
The above is to determine whether it is an integer type of five ways, these five ways each have advantages and disadvantages, we can compare carefully, choose the best to use.
Whether the five JS judgments are integers (RPM)