Five JS to determine whether it is an integer type method _javascript skill

Source: Internet
Author: User

This article looks at how to judge an integer type (integer), does not differentiate between integers and floating-point numbers in JavaScript, and all digits are represented in a 64-bit floating-point format, similar to the Java double type. In practice, for example, array indexes and bit operations are based on 32-bit integers.
method One, using the remainder operator to judge
Any integer will be divisible by 1, that is, the remainder is 0. Use this rule to determine whether an integer.

function Isinteger (obj) {return
 obj%1 = = 0
}
isinteger (3)/True
Isinteger (3.3)//False

The above output can see that this function is very useful, but for strings and some special values appear powerless

Isinteger (')//True
Isinteger (' 3 ')//True
Isinteger (True)//True
Isinteger ([])//True

It is difficult to accept the return of true for an empty string, a string type number, a Boolean true, or an empty array. Please refer to these types of internal conversion details: The false value of the wonderful flowers 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, it's perfect.
method Two, use Math.Round, Math.ceil, Math.floor judge
integers are equal to themselves after rounding. Use this feature to determine whether 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 masks the string, true,[], with less code than the previous function.
Way Three, through parseint judgment

function Isinteger (obj) {return
 parseint (obj, ten) = = obj
}
isinteger (3)//True
Isinteger (3.3)// False
Isinteger (')//False
Isinteger (' 3 ')//False
Isinteger (TRUE)//False
Isinteger ([])// False

It's good, but there's one drawback.

Isinteger (1000000000000000000000)//False

Actually returned false, no justice. The reason is that parseint forces the first parameter to be parsed into a string before parsing the integer. This method of converting numbers to integers is not a good choice.
method Four, through the bit operation judgment

function Isinteger (obj) {return
 (obj | 0) = = obj
}
isinteger (3)//True
Isinteger (3.3)//False
IsI Nteger (')//False
Isinteger (' 3 ')//False
Isinteger (TRUE)//False
Isinteger ([])//False

This function is very good, the efficiency is very high. But there is a flaw, mentioned above, the bitwise operation can only deal with the number of 32 digits, for more than 32-bit powerless, such as

Copy Code code as follows:
Isinteger (Math.pow (2, 32))//32 digits above return false

Of course, most of the time we don't use that big number.
Mode V, ES6 provides the 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 have been supported.

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

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.