JavaScript Number Values and Precision Limits
Quick answer
Number represents both integers and fractional numbers. It uses binary64 floating-point representation, which means not every mathematical value can be represented exactly. Number also includes NaN, positive and negative Infinity and signed zeros.
How it works
Integers from -(2^53 - 1) through 2^53 - 1 are safe integers. Beyond that range, adjacent integers can round to the same Number. Fractions such as 0.1 are approximated in binary. Use Number.isSafeInteger(value) when an algorithm requires reliably distinguishable integer inputs; the numeric chapter explains rounding and precision in depth.
console.log(0.1 + 0.2);console.log(0.1 + 0.2 === 0.3);Expected output in Node.js:
0.30000000000000004falseThe intermediate values are rounded binary numbers.
console.log(Number.isSafeInteger(9007199254740991));console.log(Number.isSafeInteger(9007199254740992));Expected output in Node.js:
truefalseThe second integer is representable but not safe.
Common mistakes
Do not use exact fractional equality for approximate quantities without considering an appropriate tolerance. For a bounded integer-cent total, validate safe integer inputs and safe results rather than assuming Number handles arbitrary-size integers.
Practice
Given an integer or fractional numeric input, return whether it is a safe integer.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Number Values and Precision Limits — Knowledge Check
Apply the taught concept and reason about the result.
What does Number.isSafeInteger(9007199254740992) return?
Why can 0.1 + 0.2 differ from 0.3?
Which is a Number value?
What is the largest positive safe integer?
What should an integer-cent calculator check?