In JavaScript, all numbers are double-precision 64-bit binary format IEEE 754 floating-point numbers. JavaScript does not distinguish between integers and floats at the primitive type level.
flowchart LR
BitLayout["64-Bit Float Layout"] --> Sign["1 Bit: Sign (+/-)"]
BitLayout --> Exponent["11 Bits: Exponent"]
BitLayout --> Mantissa["52 Bits: Mantissa / Fraction"]
| Constant / Limit | Value | Explanation |
|---|---|---|
Number.MAX_SAFE_INTEGER |
9,007,199,254,740,991 ($2^{53} - 1$) |
Maximum integer that can be safely represented without rounding. |
Number.MIN_SAFE_INTEGER |
-9,007,199,254,740,991 |
Minimum safe integer limit. |
| Floating Point Trap | 0.1 + 0.2 === 0.30000000000000004 |
Binary floating-point arithmetic inaccuracy for decimal fractions. |
| Special Values | NaN, Infinity, -Infinity |
Numeric calculation overflow / error results. |
// Demonstrating JavaScript numeric behavior, limits, and floating point issues
// 1. The Classic Floating Point Precision Issue
const sum = 0.1 + 0.2;
console.log(`0.1 + 0.2 = ${sum}`); // 0.30000000000000004
console.log(`Direct comparison (0.1 + 0.2 === 0.3): ${sum === 0.3}`); // false
// Solution: Epsilon tolerance comparison
const isNearlyEqual = Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON;
console.log(`Epsilon Comparison Safe Equal: ${isNearlyEqual}`); // true
// 2. Safe Integers vs Overflow
console.log(`Max Safe Integer: ${Number.MAX_SAFE_INTEGER}`);
const unsafeInt = Number.MAX_SAFE_INTEGER + 2;
console.log(`Unsafe int computation: ${unsafeInt}`); // Precision lost!
// 3. Special Numeric Values: NaN and Infinity
console.log(`1 / 0: ${1 / 0}`); // Infinity
console.log(`"abc" * 5: ${"abc" * 5}`); // NaN (Not a Number)
// Checking NaN safely
console.log(`Number.isNaN("abc" * 5): ${Number.isNaN("abc" * 5)}`); // true
Number.isNaN() Instead of isNaN(): isNaN("abc") coerces "abc" to a number first, returning true. Number.isNaN("abc") checks strictly without coercion.toFixed(2) or Intl.NumberFormat: Never calculate money directly using float additions without rounding or cents conversion.BigInt for Large Integers: For integers larger than $2^{53} - 1$, use BigInt.Explain why NaN === NaN evaluates to false in JavaScript, and how to properly check if a variable is NaN.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.