Sub topic 1
One Number Type: How JavaScript Stores Numbers
Most languages have separate types for integers and decimals. JavaScript has one type, Number, and every Number is stored as a 64-bit IEEE 754 double-precision floating-point value. The precision surprises, special values, and limits covered later in this tutorial all come from that one design choice.
Prerequisites
This chapter assumes you can:
- Write basic JavaScript with variables, functions, and operators.
- Run code in a browser console or Node.js.
- Use strict equality (
===) andtypeof.
One Type for Integers and Fractions
JavaScript does not store 42 as an integer and 42.0 as a float. Both are the same double-precision value:
console.log(typeof 42); // "number"
console.log(typeof 3.14); // "number"
console.log(42 === 42.0); // true
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // 5e-324 (smallest positive value)
console.log((0.1).toFixed(20)); // "0.10000000000000000555"The last line shows that the literal 0.1 is not stored exactly. JavaScript keeps the closest binary value it can represent, and that value is slightly more than one tenth.
The 64-Bit Layout
A double divides its 64 bits into three fields:
| Field | Bits | Purpose |
|---|---|---|
| Sign | 1 | 0 for positive, 1 for negative |
| Exponent | 11 | Power of two, stored with a bias of 1023 |
| Fraction (mantissa) | 52 | Significant digits after an implied leading 1 |
For ordinary values, the number equals (-1)^sign × 1.fraction × 2^(exponent − 1023). The implied leading 1 gives 53 bits of precision, which is roughly 15 to 17 significant decimal digits.
You can inspect the bits directly with a DataView:
function bits(x) {
const view = new DataView(new ArrayBuffer(8));
view.setFloat64(0, x);
const b = view.getBigUint64(0).toString(2).padStart(64, "0");
return `${b[0]} ${b.slice(1, 12)} ${b.slice(12)}`;
}
console.log(bits(1)); // 0 01111111111 0000000000000000000000000000000000000000000000000000
console.log(bits(-2)); // 1 10000000000 0000000000000000000000000000000000000000000000000000
console.log(bits(0.1)); // 0 01111111011 1001100110011001100110011001100110011001100110011010The value 0.1 is 1/16 × 1.6. In binary, 1.6 has a repeating pattern, 1001 1001 ..., so it has to be cut off and rounded to fit in the 52 fraction bits. The final 1010 is where that rounding happened.
What This Design Implies
- Integers are exact only up to 2^53. Beyond that, the fraction field runs out of bits. The Safe Integer Range chapter covers this limit.
- Most decimal fractions are approximations. Values such as 0.1, 0.2, and 0.3 have no exact binary form. See the chapter on why
0.1 + 0.2 !== 0.3. - Some exponent values are reserved. They encode
NaN,Infinity, and-0, which have their own chapter. - Bitwise operators and BigInt work differently. Bitwise operators briefly convert values to 32-bit integers, and
BigIntis a separate type for exact large integers.
Exercise
Using the bits() function above, what is the 11-bit exponent field for the number 4, and why?
Show answer
10000000001. 4 is 1.0 × 2^2, so the stored exponent is 2 + 1023 = 1025, which is 10000000001 in binary. The sign bit is 0 and all 52 fraction bits are 0.
Exercise
Why does typeof 7 === typeof 7.5 evaluate to true?
Show answer
JavaScript has one numeric type for both integers and fractions. Both values are 64-bit IEEE 754 doubles, so typeof returns "number" for each.