• JavaScript Video Tutorials

JavaScript Number isSafeInteger() Method



The JavaScript Number isSafeInteger() method is used to determine whether the passed value is a number that is a safe integer or not. This method returns a boolean value 'true' if a number is a 'safe integer'; otherwise it returns 'false'.

What is Safe Inetger ?

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 that lie between the -(2^53) to (2^53) exclusive, and we can represent it in the standard way.

Syntax

Following is the syntax of JavaScript Number isSafeInteger() method −

Number.isSafeInteger(testValue)

Parameters

This method accepts a parameter named 'testValue'. The same is described below −

  • testValue − The value to be checked for safe integer.

Return value

This method returns 'true' if number is a safe integer; 'false' otherwise.

Example 1

The following program demonstrates the usage of the JavaScript Number isSafeInteger() method. This method returns 'false' if we don't pass any value to it.

<html>
<head>
<title>JavaScript isSafeInteger() Method</title>
</head>
<body>
<script>
   //without an argument
   document.write("Result = ", Number.isSafeInteger());
   //output will be 'false'
</script>
</body>
</html>

Output

The above program returns 'false'.

Result = false

Example 2

If the passed value is a 'safe integer', this method returns 'true'.

The following program uses the Number.isSafeInteger() method to check if '30' is a 'safe integer'.

<html>
<head>
<title>JavaScript isSafeInteger() Method</title>
</head>
<body>
<script>
   let value = 30;
   document.write("Given number = ", value);
   document.write("<br>Is number ", value , " is a 'safe ineter' ? ", Number.isSafeInteger(value));
</script>
</body>
</html>

Output

Once the above program is executed, it will return 'true' in the output.

Given number = 30
Is number 30 is a 'safe ineter' ? true

Example 3

The method returns 'false' when the argument is not a 'safe integer'.

Here is another example of the JavaScript Number isSafeInteger() method. We check if 2 ** 53 is a 'safe integer' by passing it as an argument to this method.

<html>
<head>
<title>JavaScript isSafeInteger() Method</title>
</head>
<body>
<script>
   let value = 2 ** 53;
   document.write("Give value = ", value);
   document.write("<br>Is number ", value , " is a 'safe integer' ? ", Number.isSafeInteger(value));
</script>
</body>
</html>

Output

After executing the above program, it will return 'false' in the output.

Give value = 9007199254740992
Is number 9007199254740992 is a 'safe integer' ? false

Example 4

Let's see what happens when we pass 'NaN' and 'Infinity' as an argument to this method, one at a time.

<html>
<head>
<title>JavaScript isSafeInteger() Method</title>
</head>
<body>
<script>
   let val1 = NaN;
   let val2 = Infinity;
   document.write("Values are: ", val1 , " and ", val2);
   document.write("<br>Is value '", val1 , "' is a 'safe integer' ? ", Number.isSafeInteger(val1));
   document.write("<br>Is value '", val2 , "' is a 'safe integer' ? ", Number.isSafeInteger(val2));
</script>
</body>
</html>

Output

The above program returns 'false' when either 'NaN' or 'Infinity' is passed as an argument.

Values are: NaN and Infinity
Is value 'NaN' is a 'safe integer' ? false
Is value 'Infinity' is a 'safe integer' ? false
Advertisements