Five js ways to judge whether it is an integer type

Five js ways to judge whether it is an integer type

This article looks at how to determine the Integer type. JavaScript does not distinguish between integers and floating-point numbers. All numbers are represented in 64 bit floating-point format, which is the same as the double type of Java. However, in practical operations, such as array index and bit operations, they are based on 32-bit integers.

Method 1: use the remainder operator to judge

Any integer will be divided 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 

From the above output, we can see that this function is very useful, but it is not enough for strings and some special values

isInteger('') // true
isInteger('3') // true
isInteger(true) // true
isInteger([]) // true

It's hard to accept that empty strings, string type numbers, Boolean true and empty arrays all return true. For those interested in the details of these types of internal transformations, please refer to: wonderful false values in JavaScript
Therefore, you need to judge whether the next 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

It's hard to accept that empty strings, string type numbers, Boolean true and empty arrays all return true. For those interested in the details of these types of internal transformations, please refer to: wonderful false values in JavaScript
Therefore, you need to judge whether the next 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, that's perfect.
Method 2: use math round,Math.ceil,Math.floor judgment
The integer is still equal to itself after rounding. Use this feature to determine whether it is an integer, math An example of a floor 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 masks the string, true, [], and the amount of code is less than that of the previous function.
Method 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 also a disadvantage

isInteger(1000000000000000000000) // false

It's unreasonable to return false. The reason is that parseInt forces the first parameter to be parsed into a string before parsing an integer. This method of converting numbers to integers is not a good choice.  

Mode 4: judgment by bit operation

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 efficient. However, there is a defect. As mentioned above, bit operation can only handle numbers within 32 bits, and there is nothing to do with more than 32 bits, such as

The copy code is as follows:
Isinteger (math. Pow (2, 32)) / / a number above 32 bits returns false


Of course, most of the time we don't use such large numbers.
Mode 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

At present, the latest Firefox and Chrome have been supported.

The above are the five ways to judge whether it is an integer type. These five ways have their own advantages and disadvantages. You can carefully compare them and choose the best one for use.

Keywords: Javascript

Added by belick on Thu, 30 Dec 2021 00:17:04 +0200