Computer >> Computer tutorials >  >> Programming >> Javascript

How to check whether a value is a safe integer or not in JavaScript?


A safe integer is an integer represented as an IEEE-754 double-precision number. That would mean integers from (253 - 1) to -(253 - 1). To check whether a value is a safe integer or not in JavaScript, use the isSafeInteger() method.

Example

You can try to run the following code to check whether a value is a safe integer or not. It returns true for a safe integer and integer −

<!DOCTYPE html>
<html>
   <body>
      <script>
         document.write("Checking for Safe Integer...");
         document.write("<br>"+Number.isSafeInteger(765));
         document.write("<br>"+Number.isSafeInteger(-765));
         document.write("<br>"+Number.isSafeInteger('765'));
         document.write("<br>"+Number.isSafeInteger(false));
         document.write(Number.isSafeInteger(Math.pow(2, 53) - 1));
      </script>
   </body>
</html>