Five kinds of JS to determine whether an integer

Source: Internet
Author: User

Whether the five JS judgments are integer type

Snandy

This article mainly introduces five kinds of JavaScript to determine whether it is an integer type, the need for friends can refer to the following

This article looks at how to determine the integer type (integer), which does not distinguish between integers and floating-point numbers in JavaScript, and all digits are represented in a 64-bit floating-point format, just like Java's double type. But the actual operation, such as array index, bit operation is based on 32-bit integers.
Method one, using the take-up operator to judge
Any integer will be divisible by 1, i.e. the remainder is 0. Use this rule to determine if it is an integer.

?
12345 functionisInteger(obj) { returnobj%1 === 0}isInteger(3) // trueisInteger(3.3) // false 

The above output shows that this function is very useful, but for strings and some special values appear to be inadequate

?
1234 isInteger(‘‘) // trueisInteger(‘3‘) // trueisInteger(true) // trueisInteger([]) // true

It is unacceptable for empty strings, String type numbers, Boolean true, and empty arrays to return true. For more information on these types of internal conversion details, please refer to: False values of wonderful flowers in JavaScript
Therefore, you need to first determine whether the object is a number, such as adding a typeof

?
1234567 function isinteger (obj) {   return Code class= "JS keyword" >typeof obj = = && obj%1 = = 0 } isinteger ( " //false isinteger ( ' 3 ' ) //false isinteger ( true //false isinteger ([]) //false

Well, it's more perfect.
Method Two, use Math.Round, Math.ceil, Math.floor judgment
Integers are equal to themselves after rounding. Use this feature to determine if it is an integer, math.floor example, as follows

?
123456789 function isinteger (obj) {   return math.floor (obj) = = = obj } isinteger (3) / /true isinteger (3.3) //false isinteger ( " //false isinteger ( ' 3 ' ) //false isinteger ( true Code class= "JS plain" > //false isinteger ([]) //false

This directly blocks the string, true,[], and has less code than the previous function.
Method Three, judge by parseint

?
123456789 functionisInteger(obj) { returnparseInt(obj, 10) === obj}isInteger(3) // trueisInteger(3.3) // falseisInteger(‘‘) // falseisInteger(‘3‘) // falseisInteger(true) // falseisInteger([]) // false

Very good, but there is a disadvantage

?
1 isInteger(1000000000000000000000) // false

Unexpectedly return false, no justice AH. The reason is that parseint forces the first argument to be parsed into a string before parsing the integer. This method is not a good choice for converting numbers into integers.
Method Four, judge by the bit operation

?
123456789 function isinteger (obj) {   return (obj | 0) = = = Obj } isinteger (3) // True isinteger (3.3) //false isinteger ( " //false isinteger ( ' 3 ' Code class= "JS Plain" >) //false isinteger ( Code class= "JS keyword" >true //false isinteger ([]) //false

This function is very good, the efficiency is very high. But there is a flaw, as mentioned above, that bit arithmetic can only handle numbers within 32 digits, for more than 32 bits of powerless, such as

Copy CodeThe code is as follows: Isinteger (Math.pow (2, 32))//32 digits above returns false
Of course, most of the time we don't use that big number.
Way Five, ES6 provided the Number.isinteger

?
123456 Number.isInteger(3) // trueNumber.isInteger(3.1) // falseNumber.isInteger(‘‘) // falseNumber.isInteger(‘3‘) // falseNumber.isInteger(true) // falseNumber.isInteger([]) // false

Currently, the latest Firefox and Chrome have been supported.

The above is to determine whether it is an integer type of five ways, these five ways each have advantages and disadvantages, we can compare carefully, choose the best to use.

Whether the five JS judgments are integers (RPM)

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.