Multiple methods of judging integers in JavaScript summarizing _javascript techniques

Source: Internet
Author: User

This article looks at how to judge an integer type by recording a number of ways in JavaScript that are judged to be numeric types.

JavaScript does not differentiate between integers and floating-point numbers, 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.

Copy Code code as follows:

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

Copy Code code as follows:

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

Copy Code code as follows:

function Isinteger (obj) {
return typeof obj = = ' number ' && obj%1 = 0
}
Isinteger (')//False
Isinteger (' 3 ')//False
Isinteger (True)//False
Isinteger ([])//False

Well, it's perfect.

Second, the use of Math.Round, Math.ceil, Math.floor judgment

Integers are equal to themselves after rounding. Use this feature to determine whether an integer, math.floor example, as follows

Copy Code code 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.

Third, through parseint judgment

Copy Code code as follows:

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.
Copy Code code as follows:

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.

Four, through the position operation judgment

Copy Code code 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, 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.

V. ES6 provided the Number.isinteger

Copy Code code 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 have been 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.