Home  >  Article  >  Web Front-end  >  Five js ways to determine whether it is an integer type_javascript skills

Five js ways to determine whether it is an integer type_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:28:061269browse

This article looks at how to determine 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. However, actual operations such as array indexing and bit operations are based on 32-bit integers.
Method 1, use 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.
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 strings, true, and [], and the amount of code is less than 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 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.
Method 4: Determine 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

Copy code The code is as follows:
isInteger(Math.pow(2, 32)) // A number of more than 32 digits Returned false

Of course, most of the time we won't use numbers that big.
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 already support it.

The above are the five ways to determine whether it is an integer type. Each of these five ways has its own advantages and disadvantages. You can compare them carefully and choose the best one for use.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn