• JavaScript Video Tutorials

JavaScript Number MAX_SAFE_INTEGER Property



The JavaScript Number MAX_SAFE_INTEGER is a data static property, that represents the maximum safe integer. The 'maximum safe integer' value in JavaScript is "9007199254740991" or "253-1". Therefore, it is a property of the Number object, so you can only use it as a Number.MAX_SAFE_INTEGER.

Note: If you try to access it using x.MAX_SAFE_INTEGER, where 'x', is a variable, it will return undefined.

Syntax

Following is the syntax of JavaScript Number MAX_SAFE_INTEGER property −

Number.MAX_SAFE_INTEGER;

Parameters

  • It does not accept any parameters.

Return value

This property has no return value.

Example 1

The following program demonstrates the usage of the JavaScript Number MAX_SAFE_INTEGER property. It will return '9007199254740991' for Number.MAX_SAFE_INTEGER.

<html>
<head>
<title>JavaScript Number MAX_SAFE_INTEGER Property</title>
</head>
<body>
<script>
   document.write("Maximum safe integer: " , Number.MAX_SAFE_INTEGER);
</script>
</body>
</html>

Output

The above program returns the maximum safe integer value in JavaScript as '9007199254740991'.

Maximum safe integer: 9007199254740991

Example 2

If you try to access this property using any variable, it will return an undefined.

The following is another example of the JavaScript Number MAX_SAFE_INTEGER property. Here, we are trying to find the maximum safe integer value by using x.MAX_SAFE_INTEGER, where "x" is a variable with the value 10.

<html>
<head>
<title>JavaScript Number MAX_SAFE_INTEGER Property</title>
</head>
<body>
<script>
   let x = 10;
   document.write("x = ", x);
   document.write("<br>Maximum safe integer = ", x.MAX_SAFE_INTEGER);
</script>
</body>
</html>

Output

This will return 'undefined' for x.MAX_SAFE_INTEGER.

x = 10
Maximum safe integer = undefined

Example 3

Let's test what happens if we try to add an integer value to the result of Number.MAX_SAFE_INTEGER, whether it returns the default maximum safe integer value or the actual value after adding an integer.

<html>
<head>
<title>JavaScript Number MAX_SAFE_INTEGER Property</title>
</head>
<body>
<script>
   let val = 2;
   document.write("val = ", val);
   document.write("<br>Maximum safe integer without adding = ", Number.MAX_SAFE_INTEGER);
   document.write("<br>Maximum safe integer after adding 2 = ", Number.MAX_SAFE_INTEGER + val);
   //lets compare the value of m1 and m2
   const m1 = Number.MAX_SAFE_INTEGER + 1;
   const m2 = Number.MAX_SAFE_INTEGER + 2;
   document.write("<br>m1 = ", m1);
   document.write("<br>m2 = ", m2);
   document.write("<br>m1 === m2: ");
   if(m1 === m2){
      document.write("True");
   }
   else{
      document.write("False");
   }
</script>
</body>
</html>

Output

There will be a loss of precision due to rounding and safe integer overflow. Both m1 and m2 will equal 9007199254740992, and will be evaluated as true.

val = 2
Maximum safe integer without adding = 9007199254740991
Maximum safe integer after adding 2 = 9007199254740992
m1 = 9007199254740992
m2 = 9007199254740992
m1 === m2: True
Advertisements