Five js ways to judge whether it is an integer type

This article takes a look at how to judge the integer type (Integer). JavaScript does not distinguish between integers and floating-point numbers. All numbers are internally represented in 64-bit floating-point format, which is the same as Java's double type. But practical operations such as array indexing and bit operations are based on 32-bit integers.

Method 1. Use the remainder operator to judge

Any integer is divisible by 1, i.e. 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 useful, but it is not enough for strings and some special values.

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

Returns true for empty strings, string type numbers, boolean true, and empty arrays, which is really unacceptable. If you are interested in the details of the internal conversion 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 perfect.

Method 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 the strings, true, [], and the amount of code is less than the previous function.

Method 3: Judging 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
很不错,但也有一个缺点
isInteger(1000000000000000000000) // false

It returned false, which is unreasonable. The reason is that parseInt forces the first argument to be parsed as a string before parsing the integer. Converting numbers to integers this way is not a good choice.

Method 4: Judging 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 the efficiency is very high. But there is a flaw. As mentioned above, bit operations can only process numbers within 32 bits, and can do nothing for more than 32 bits. For example, the code for copying the code is as follows:

isInteger(Math.pow(2, 32)) // 32位以上的数字返回false了

Of course, most of the time we don't use such large numbers.

Method 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 are already supported.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325861642&siteId=291194637