Lesson

JS Numbers

10 min reading
Free Course

JavaScript Numbers: Floating-Point Precision and Limits

JavaScript has one standard number type for both integers and decimals, stored in 64-bit IEEE 754 floating-point format. Numbers also include three special values: Infinity, -Infinity, and NaN (Not a Number).

Safe Integer Limits and Numeric Separators

Because 64-bit floats reserve bits for exponents and signs, exact integer math works reliably up to Number.MAX_SAFE_INTEGER (9,007,199,254,740,991). Above that boundary, consecutive integers lose precision and round to even values.

Modern JavaScript (ES2021) also lets you write underscores inside large numbers as visual separators, such as 1_000_000, which engines ignore during execution.

Handling Precision, NaN, and Infinity

Always use Number.isNaN() instead of global isNaN(), because global isNaN("abc") coerces strings first and returns misleading results.

const annualBudget = 1_500_000;
const badMath = Number("abc"); // NaN

console.log("Budget:", annualBudget);
console.log("Is NaN strictly:", Number.isNaN(badMath)); // true
console.log("NaN === NaN:", badMath === badMath);       // false!

// Fix binary decimal drift when comparing floats
const sum = 0.1 + 0.2;
const isClose = Math.abs(sum - 0.3) < Number.EPSILON;
console.log("0.1 + 0.2 close to 0.3:", isClose);

Common Mistakes to Avoid

  • Checking x === NaN: NaN is the only value in JavaScript not equal to itself. Always call Number.isNaN(x).
  • Comparing floats directly with ===: 0.1 + 0.2 === 0.3 evaluates to false due to binary representation. Use integer cents or compare against Number.EPSILON.
  • Using leading zeros like 075: Older octal syntax causes confusion or throws a SyntaxError in strict mode. Use 0o75 for explicit octal numbers.

Frequently Asked Questions

Why does typeof NaN return "number"?

In IEEE 754 math, NaN represents an invalid numeric result inside the number type, such as 0 / 0 or Number("hello").

What is the difference between isNaN() and Number.isNaN()?

Global isNaN("hello") converts "hello" to NaN and returns true. Number.isNaN("hello") returns false because "hello" is a string, not the NaN value.

When should I switch from Number to BigInt?

Switch to BigInt whenever you handle integers larger than 9,007,199,254,740,991, such as 64-bit database IDs or cryptography.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
Lesson Recap Quiz Available

Test Your Knowledge

You've completed this section! Take a quick 5-question quiz to check your understanding.

JavaScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum