Multiple methods for determining integers in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces various methods to judge integers in JavaScript. This article summarizes five methods to judge integers, such as the remainder operator judgment and Math. round, Math. ceil, Math. floor judgment and so on. For more information, see the various methods that have been recorded in JavaScript to identify as numbers. In this article, let's take a look at how to determine as Integer ).

JavaScript does not distinguish between integers and floating-point numbers. All numbers are expressed in 64-bit floating-point format, which is the same as Java's double type. However, in actual operations, for example, array indexes 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.

The Code is as follows:


Function isInteger (obj ){
Return obj % 1 = 0
}
IsInteger (3) // true
Isintegers (3.3) // false

The above output shows that this function is quite useful, but it cannot be used for strings or some special values.

The Code is as follows:


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

True is returned for null strings, string-type numbers, Boolean true, and empty arrays, which is really unacceptable. For details about the internal conversion of these types, see the odd false values in JavaScript.

Therefore, you need to first http://www.jb51.net/article/57222.htm "> to determine whether the object is a number, such as adding a typeof

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 perfect.

2. Use Math. round, Math. ceil, and Math. floor to Judge

The integer is equal to itself after being rounded. This feature is used to determine whether it is an integer. The Math. floor example is as follows:

The Code is as follows:


Function isInteger (obj ){
Return Math. floor (obj) === obj
}
IsInteger (3) // true
Isintegers (3.3) // false
IsInteger ('') // false
IsInteger ('3') // false
IsInteger (true) // false
IsInteger ([]) // false


This directly blocks the string, true, [], with less code than the previous function.

Iii. parseInt-based judgment

The Code is as follows:


Function isInteger (obj ){
Return parseInt (obj, 10) === obj
}
IsInteger (3) // true
Isintegers (3.3) // false
IsInteger ('') // false
IsInteger ('3') // false
IsInteger (true) // false
IsInteger ([]) // false


Very good, but there is also a drawback

The Code is as follows:


Isintegers (1000000000000000000000) // false


It turns out that false is returned. It's not reasonable. The reason is that parseInt forces the first parameter to be parsed into a string before the integer is parsed. This method is not a good choice to convert a number into an integer.

4. bitwise operation

The Code is as follows:


Function isInteger (obj ){
Return (obj | 0) = obj
}
IsInteger (3) // true
Isintegers (3.3) // false
IsInteger ('') // false
IsInteger ('3') // false
IsInteger (true) // false
IsInteger ([]) // false


This function is very good and efficient. However, there is a flaw. As mentioned above, bitwise operations can only process numbers within 32 bits, and there is nothing to do with the number exceeding 32 bits, as shown in figure

The Code is as follows:


IsInteger (Math. pow (2, 32) // if the number is greater than 32 bits, false is returned.


Of course, most of the time we don't use that big number.

5. ES6 provides Number. isInteger

The Code is as follows:


Number. isInteger (3) // true
No. 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 supported.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.