Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

JavaScript Number Min & Max & Safe Values

The JavaScript number data type has its limitation regarding Max Value, Safe Value & Min Value that you can use. It uses the MAX_SAFE_INTEGER, MIN_SAFE_INTEGER, MAX_VALUE & MIN_VALUE to represent these values.

The JavaScript stores the numbers as a double-precision 64-bit number, which means that it has a total 64-bit memory allocated to it. These numbers are stored using the scientific notation in binary format.

Useful References

Out of these 64 bits, one bit is used for the sign, 11 for the exponent and remaining 52 bits are used for the mantissa (significand).

Useful References
  • Sign bit indicates whether the number is positive or negative. The 0 stands for positive
  • Significand contains the digits of the number.
  • The exponent tells us the position of the point

Convert Decimal to Binary 64 Bit & Vice Versa

You can follow these links to find out how to

  1. convert decimal number to binary 64-bit
  2. convert binary 64-bit number to decimal

MAX_SAFE_INTEGER & MIN_SAFE_INTEGER

The Number.MAX_SAFE_INTEGER constant represents the maximum safe integer that you can use in JavaScript. It is the static property of the Number object. It has the value of 9007199254740991.

It is referred to as safe because any number more than the above number is not guaranteed to be represented exactly and correctly. This is not the limitation of the JavaScript, but a limitation imposed by the double-precision 64-bit number format followed by Javascript

                            
console.log(Number.MAX_SAFE_INTEGER)    //9007199254740991
console.log(Number.MIN_SAFE_INTEGER)    //-9007199254740991
 
 
*** output ****
9007199254740991
-9007199254740991
                            
                        
                            
                        

9007199254740991 is represented

                            
 0-10 00011 0011-1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111
                            
                        
                            
                        

Any number above this number will result in loss of accuracy. For Example, adding 1 & 2 to the MAX_SAFE_INTEGER results in the same value

                             
console.log(Number.MAX_SAFE_INTEGER+1)
console.log(Number.MAX_SAFE_INTEGER+2)
 
//*** output **
//9007199254740992        
//9007199254740992
                            
                        
                            
                        

And that is because of both converts to same in 64-bit binary

                             
9007199254740992
0-100 0011 0100-0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
 
9007199254740993
0-100 0011 0100-0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
 
9007199254740994
0-100 0011 0100-0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 000 00001
                            
                        
                            
                        

isSafeInteger

You can use the Number.isSafeInteger method to check whether the number is safe.

                            
console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER));
console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER+1));
 
//*** output **
//true
//false
                        
                            
                        

MAX_VALUE & MIN_VALUE

The largest number possible to represent using the number data type is 1.7976931348623157e+308 and it is represented by Number.MAX_VALUE. The lowest number is Number.MIN_VALUE.

                            
console.log(Number.MAX_VALUE)
console.log(Number.MIN_VALUE)
 
****** Output *****
1.7976931348623157e+308
5e-324
        
                        
                            
                        

Numbers beyond Number.MAX_VALUE cannot be represented using the double-precision 64-bit binary system. Hence any value above the MAX_VALUE is truncated to the MAX_VALUE.

                            
 console.log(Number.MAX_VALUE + 100 == Number.MAX_VALUE);  
 
 //**output
 //true
        
                        
                            
                        

While the very large value results in infinity

                            
console.log(Number.MAX_VALUE + Number.MAX_VALUE);   
 
 **output
 //Infinity
        
                        
                            
                        

Numbers & Bitwise operators

The bitwise operators and shift operators operate on 32-bit integers only, so in that case, the max safe integer is 2147483647.

Summary

The JavaScript Number object has static properties MAX_SAFE_INTEGER, MIN_SAFE_INTEGER, MAX_VALUE & MIN_VALUE. These represent the Maximum/Minimum values that are supported by the number data type. The MAX_SAFE_INTEGER & MIN_SAFE_INTEGER are extremely important, as any number above these numbers are not guaranteed to be represented exactly and correctly.

Read More

  1. JavaScript Tutorial
  2. Data Types in JavaScript
  3. JavaScript String
  4. Template Strings & String Interpolation
  5. Tagged Templates
  6. String to Number
  7. String
  8. Infinity
  9. BigInt
  10. BintInt Vs Number
  11. Boolean Data Type
  12. Undefined
  13. null
  14. null vs undefined