Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check whether a value is a safe integer or not in JavaScript?
In this tutorial, we will learn to check whether a value is a safe integer or not in JavaScript. A safe integer in JavaScript is any number that can be accurately represented under the IEEE-754 double-precision format. Safe integers are all numbers between -(2^53 - 1) and (2^53 - 1) inclusive.
JavaScript provides a built-in method for this check, but we can also implement custom logic. Here are the approaches we'll explore:
Using the Number.isSafeInteger() Method (Recommended)
Using Custom if-else Logic
Using Number.isSafeInteger() Method
The Number.isSafeInteger() method is the standard way to check if a value is a safe integer. It verifies that the value is a number and falls within the safe integer range.
Syntax
Number.isSafeInteger(value)
Parameters
value ? The value to test for being a safe integer
Return Value
Returns true if the value is a safe integer, false otherwise.
Example
Let's test Number.isSafeInteger() with different data types and edge cases:
<html>
<head>
</head>
<body>
<h2>Check if value is safe Integer using Number.isSafeInteger()</h2>
<div id="output"></div>
<script>
var output = document.getElementById("output");
// Test various values
output.innerHTML += "1000 is safe integer: " + Number.isSafeInteger(1000) + "<br>";
output.innerHTML += "true is safe integer: " + Number.isSafeInteger(true) + "<br>";
output.innerHTML += "'Hello' is safe integer: " + Number.isSafeInteger("hello") + "<br>";
output.innerHTML += "Math.pow(2,53) is safe integer: " + Number.isSafeInteger(Math.pow(2, 53)) + "<br>";
output.innerHTML += "123.43 is safe integer: " + Number.isSafeInteger(123.43) + "<br>";
output.innerHTML += "-90 is safe integer: " + Number.isSafeInteger(-90) + "<br>";
output.innerHTML += "Number.MAX_SAFE_INTEGER is safe: " + Number.isSafeInteger(Number.MAX_SAFE_INTEGER) + "<br>";
output.innerHTML += "Number.MAX_SAFE_INTEGER + 1 is safe: " + Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1) + "<br>";
</script>
</body>
</html>
1000 is safe integer: true true is safe integer: false 'Hello' is safe integer: false Math.pow(2,53) is safe integer: false 123.43 is safe integer: false -90 is safe integer: true Number.MAX_SAFE_INTEGER is safe: true Number.MAX_SAFE_INTEGER + 1 is safe: false
Using Custom if-else Logic
We can implement our own safe integer check using conditional statements. This approach manually verifies the type and range conditions that Number.isSafeInteger() performs internally.
Syntax
if (typeof value === 'number' && Number.isInteger(value) &&
value >= Number.MIN_SAFE_INTEGER && value
Example
Here's a custom function that replicates the behavior of Number.isSafeInteger():
<html>
<head>
</head>
<body>
<h2>Check if value is safe Integer using custom logic</h2>
<div id="output"></div>
<script>
var output = document.getElementById("output");
function customIsSafeInteger(value) {
return typeof value === 'number' &&
Number.isInteger(value) &&
value >= Number.MIN_SAFE_INTEGER &&
value
customIsSafeInteger(-90): true
customIsSafeInteger(true): false
customIsSafeInteger('yes'): false
customIsSafeInteger(123.6543): false
customIsSafeInteger(9007199254740991): true
customIsSafeInteger(9007199254740992): false
Key Points
Safe integers range from
Number.MIN_SAFE_INTEGER(-9,007,199,254,740,991) toNumber.MAX_SAFE_INTEGER(9,007,199,254,740,991)Floating-point numbers are never considered safe integers
Non-numeric values like strings and booleans return
falseBeyond safe integer limits, JavaScript loses precision in arithmetic operations
Comparison
| Method | Performance | Readability | Recommendation |
|---|---|---|---|
Number.isSafeInteger() |
Faster | High | Preferred |
| Custom if-else logic | Slower | Medium | Educational purpose |
Conclusion
Use Number.isSafeInteger() as the standard method to check for safe integers in JavaScript. The custom implementation helps understand the underlying logic but the built-in method is more efficient and reliable.
