The integer type (integer) in JavaScript often causes some strange problems. In the ECMAScript specification, they exist only in the concept:
All numbers are floating-point numbers, and integers simply do not have a group of digits without a decimal number.
In this blog, I'll explain how to check if a value is an integer.
ECMAScript 5
There are many ways you can use it in ES5. Sometimes, you might want to use your own method: a isinteger (x) function, or False if the integral type returns TRUE.
Let's take a look at some examples.
Check by remainder
You can use the remainder operation (%) to press a number to 1 to see if the remainder is 0.
function Isinteger (x) {return
x% 1 = 0;
}
I like this method, because it is very simple and effective.
> Isinteger
true
> Isinteger (17.13)
false
You have to be careful when working with the remainder, because the result depends on the first number of symbols, if it is positive, the result is positive, otherwise it is negative.
> 3.5% 1
0.5
> -3.5 1
-0.5
Then, we can also check 0, which is actually not a problem. The problem is that this method returns true for Non-numeric, because% converts it to a number:
> Isinteger (')
true
> Isinteger (') True
> Isinteger (False)
true
> Isinteger (True)
true
Can be solved by a very simple type check:
function Isinteger (x) {return
(typeof x = = ' number ') && (x 1 = 0);
}
by Math.Round () if a number is rounded up and is the same as the previous value, then it is an integer. In JavaScript, you can check by Math.Round ():
function Isinteger (x) {return
math.round (x) = = x;
}
This is a good method.
> Isinteger
true
> Isinteger (17.13)
false
It can also judge Non-numeric because Math.Round () always returns a number, and = = Returns true only if the type is the same.
If you want to make the code a little clearer, you can add type checking (what we did in previous versions). In addition, Math.floor () and Math.ceil () can work like Math.Round (). A bit-action check-bit operator provides another way to "take the whole":
function Isinteger (x) {return
(x | 0) = = x;
}
This solution, like other bit operations, has a flaw: it cannot handle more than 32 digits.
> Isinteger (Math.pow (2))
false
The parseint () check parseint () also provides a way to convert a number to an integral type, similar to Math.Round (). Let's see why this method is good.
function Isinteger (x) {return
parseint (x, ten) = = x;
}
Like the Math.Round () solution, it can also handle non-numeric situations, but it also does not handle all integer numbers correctly:
> Isinteger (1000000000000000000000)
false
Why? parseint () forces the first parameter to be parsed into a string before parsing the integer. Therefore, it is not a good choice to convert a number to an integral type using this method.
> parseint (1000000000000000000000)
1
> String (1000000000000000000000)
' 1e+21 '
Like the above, parseint () stops processing at 1 when parsing ' 1e+21 ', so it returns 1. ECMAScript 6 for Math.Round () toss, ES6 provides another way to convert numbers to integers: Math.trunc (). This function removes the decimal part of the number.
> Math.trunc (4.1)
4
> Math.trunc (4.9)
4
> Math.trunc ( -4.1)
-4
> Math.trunc (- 4.9)
-4
In addition, ECMASCRIPT6 does not need to handle the trivial tasks of checking integers because it comes with a built-in function Number.isinteger ().