Home > Web Front-end > JS Tutorial > body text

Summary of various methods for judging integers in JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:32:10
Original
1348 people have browsed it

We have previously recorded various ways to determine the numeric type in JavaScript. This article will look at how to determine the integer type (Integer).

JavaScript does not distinguish between integers and floating point numbers. All numbers are internally represented in a 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 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 code The code is as follows:

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

Copy code The code is as follows:

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

Copy code The 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 more perfect.

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

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

3. Judge by parseInt

Copy code The code is as follows:

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
Copy code The code is as follows:

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.

4. Judgment through bit operations

Copy code The code is as follows:

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)) // Numbers with more than 32 digits return false

Of course, most of the time we won't use numbers that big.

5. ES6 provides Number.isInteger

Copy code The code is as follows:

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!