JavaScript .isSafeInteger()
The .isSafeInteger() method is a static method of the Number object in JavaScript which determines whether the provided value is a safe integer: an integer that can be exactly represented using the IEEE-754 double-precision format.
A safe integer satisfies:
$$ -(2^{53} - 1) \leq \text{value} \leq 2^{53} - 1 $$
If the condition holds, the method returns true, otherwise, it returns false.
Syntax
Number.isSafeInteger(value);
Parameters:
value: The value to be tested for being a safe integer.
Return value:
Returns true if the given value is of type number, is an integer, and is within the safe integer range (-(2^53 - 1) to 2^53 - 1). Otherwise, it returns false.
Example
This example demonstrates how .isSafeInteger() returns true for integers and false for non-integers or values outside the safe range:
console.log(Number.isSafeInteger(10));console.log(Number.isSafeInteger(3.14));console.log(Number.isSafeInteger(Math.pow(2, 53)));console.log(Number.isSafeInteger(2 ** 53 - 1));
The output of this code is:
truefalsefalsetrue
Codebyte Example
This codebyte tests various values to show how the method handles whole numbers, decimals, and edge cases at the limits of JavaScript’s safe integer range:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours