JavaScript Interview Question #183

What is Number.MAX_SAFE_INTEGER?

Advanced JavaScript, Practical Problems & Senior Interview Scenarios Senior Advanced

Quick Interview Answer

Number.MAX_SAFE_INTEGER is the largest integer that JavaScript's Number type can represent while reliably distinguishing consecutive integers. Its value is 2^53 - 1.

Detailed Explanation

Number.MAX_SAFE_INTEGER is the largest integer that JavaScript's Number type can represent while reliably distinguishing consecutive integers. Its value is 2^53 - 1.

For integer values beyond the safe range, BigInt may be appropriate. For money and other precision-sensitive domains, developers should also understand floating-point limitations and choose a suitable numeric representation.

Code Example

console.log(Number.MAX_SAFE_INTEGER);
// 9007199254740991

console.log(
    Number.isSafeInteger(
        Number.MAX_SAFE_INTEGER
    )
); // true

const huge = 9007199254740992n;