JavaScript Number.isSafeInteger()
Method
Example
Check if the parameter is a "safe integer":
Definition and Usage
The isSafeInteger()
method determines whether the provided value is a "safe integer" (safe integer). It returns true if the value is a safe integer, otherwise it returns false.
A safe integer is an integer that:
- Can be exactly represented as an IEEE-754 double-precision number.
- Its IEEE-754 representation cannot be the result of rounding any other integer to fit the IEEE-754 representation.
For example, 2^53 - 1 is a safe integer, as it can be exactly represented and no other integer rounds to this number under any IEEE-754 rounding mode. In contrast, 2^53 is not a safe integer because, although it can be represented in IEEE-754, 2^53 + 1 cannot be directly represented and is rounded to 2^53 in round-to-nearest and toward-zero rounding modes.
The range of safe integers is from -(2^53 - 1) to 2^53 - 1, inclusive.
Number.isSafeInteger(3); // true
Number.isSafeInteger(Math.pow(2, 53)) // false
Number.isSafeInteger(Math.pow(2, 53) - 1) // true
Number.isSafeInteger(NaN); // false
Number.isSafeInteger(Infinity); // false
Number.isSafeInteger("3"); // false
Number.isSafeInteger(3.1); // false
Number.isSafeInteger(3.0); // true
Browser Support
Number.isSafeInteger()
is a feature of ECMAScript6 (ES6).
Most modern browsers support ES6 (JavaScript 2015).
Number.isSafeInteger()
is not supported in Internet Explorer 11 and earlier versions.
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
Syntax
Number.isSafeInteger(value)
Parameter Values
Parameter | Description |
---|---|
value | The value to be tested. |
Return Value
Type | Description |
---|---|
Boolean | Returns true if the value is a safe integer, otherwise returns false. |
Technical Details
| JavaScript Version: | ECMAScript 6 | | --- | --- |
More Examples
Example
Check if the parameter is a safe integer: