How does jquery determine whether it is an integer

How to judge whether it is an integer by jquery: 1. Use the remainder operator to judge; 2. Use "Math.round" to judge; 3. judge by parseInt; 4. judge by bit operation; 5. judge by Number.isInteger provided by ES6 Whether it is an integer.



The operating environment of this tutorial: Windows7 system, jquery1.10.0 version, this method is applicable to all brand computers.

Recommendation: jQuery video tutorial

js to determine whether it is an integer type (5 ways)

Method 1. Use the remainder operator to determine

any integer will be divisible by 1, that is, the remainder is 0. Use this rule to determine whether it is an integer.

1

2

3

4

5

6

7

8

9

function isInteger(obj) {  return obj%1 === 0 } isInteger(3) // true isInteger(3.3) // false isInteger('') // true isInteger('3 ') // true isInteger(true) // true isInteger([]) // true returns true for empty strings, string type numbers, Boolean true, and empty arrays. For those interested in the details of these types of internal conversions, please refer to: Wonderful Fake Values ​​in JavaScript



















Therefore, you need to first determine whether the object is a number, such as adding a typeof

1

2

3

4

5

6

7

function isInteger(obj) {  return typeof obj ==='number' && obj%1 === 0 } isInteger('' ) // false isInteger('3') // false isInteger(true) // false isInteger([]) // false method two, use Math.round, Math.ceil, Math.floor to determine whether the integer is equal to integer Yourself. Use this feature to determine whether it is an integer. Math.floor example is as follows: 1 2 3 4 5 6 7 8 9 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

method three, judge by parseInt

1

2

3

4

5

6

7

8

9

10

11

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 isInteger(1000000000000000000000) // false is because parseInt forces the first parameter to be parsed into a string before parsing integers. This method of converting numbers to integers is not a good choice. Method 4: Judgment 1 by bit operation



























2

3

4

5

6

7

8

9

10

11

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 efficient. But there is a flaw. As mentioned above, bit operations can only handle numbers within 32 bits, and there is nothing more than 32 bits. isInteger(Math.pow(2, 32)) // Numbers above 32 bits return false . 5. ES6 provides Number.isInteger 1 2 3 4 5 6 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 are supported.

Guess you like

Origin blog.csdn.net/nk90875/article/details/113013182