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. The simple definition of the safe integer in JavaScript is all numbers we can represent under the IEEE-754 double-precision number. It is the set of all numbers which lie between the -(2^53) to (2^53) exclusive, and we can represent it in the standard way.

Here, we have different approaches to checking whether the number is a safe integer.

• Using the Number.IsSafeInteger() Method

• Using the if-else Conditional Statement

Using the Number.isSafeInteger() Method

In JavaScript, the isSafeInteger() method checks that type of value is a number and is between the -(2^53) to (2^53). We can pass the different values as a parameter of the method, and it returns the Boolean value as a result. If the number is a safe integer, it returns true otherwise false.

Syntax

Users can use the syntax below to use the isSafeInteger() method

let isSafe = Number.isSafeInteger(value);


Parameters

• value − It is the value of any variable for which user wants to check whether value is safe integer or not.

Example

In the below example, we have used the Number.isSafeInteger() method to check whether the value is a safe integer. We checked it for different values such as Boolean, string, float, and integer

<html>
<body>
<h2>Check if value is safe Integer or not in JavaScript.</h2>
<h4>Check if value is safe Integer or not using <i> isSafeInteger() </i> method.</h4>
<div id = "output"></div>
<script>
var output = document.getElementById("output");
let value = 1000;
let isSafe = Number.isSafeInteger(value);
output.innerHTML += "1000 is safe Integer : " + isSafe + " <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/> ";
</script>
</body>
</html>


Using the if-else Conditional Statement

In this method, we will simply use the if-else statement to check whether the value is a type of number or not. If the value is a number, we will check if it is between the -(2^53) to (2^53). It is the custom logic based on that isSafeInteger() method that returns the Boolean output.

Syntax

The syntax to check whether values is safe integer or not using the if-else statement is given below.

if ( typeof value === 'number' && -Math.pow(2, 53) < value && Math.pow(2, 53) > value ) {

// value is safe integer
} else {

// value is not safe integer
}

Example

In the example below, we have simply implemented the above method. We have created the function named safeInteger(), which checks for the safe integer according to the method explained above and returns the Boolean value.

<html>
<body>
<h2>Check if value is safe Integer or not in JavaScript.</h2>
<h4>Check if value is safe Integer or not using <i> if-else</i> statement.</h4>
<div id = "output"></div>
<script>
var output = document.getElementById("output");
function safeInteger(value) {
if ( typeof value === 'number' && -Math.pow(2, 53) < value &&Math.pow(2, 53) > value ) {
return true;
} else {
return false;
}
}
output.innerHTML += " safeInteger (-90) : " + safeInteger(-90) + " <br/> ";
output.innerHTML += " safeInteger (true) : " + safeInteger(true) + " <br/> ";
output.innerHTML += " safeInteger ('yes') : " + safeInteger('yes') + " <br/> ";
output.innerHTML += " safeInteger (123.6543) : " + safeInteger(123.6543) + " <br/> ";
</script>
</body>
</html>


We have learned two different approaches to checking whether the value is a safe integer is not. In the first approach, we used the built-in method; in the second approach, we implemented the first method from scratch.

Updated on: 08-Aug-2022

437 Views