liveBook · Manning

Number type

As explained in JavaScript in Depth

The JavaScript built-in numeric type that represents values primarily as IEEE 754 double-precision floating-point numbers.

Why does 0.1 + 0.2 fail an equality check?

`0.1 + 0.2` evaluates to `0.30000000000000004`, so `0.1 + 0.2 === 0.3` is false. The discrepancy is a consequence of representing decimal fractions in binary floating point, not an arithmetic bug. section 3.0

A JavaScript Number is specified as an IEEE 754-2019 double-precision, 64-bit floating-point value. Its bits encode a sign, an exponent, and a significand. Decimal notation must be converted to that representation; fractions such as 0.1 and 0.2 have repeating binary expansions, so their stored values are approximations. Adding those approximations and rounding the result produces the surprising value above. section 3.1 section 3.1.1

Figure 3.2 The “double-precision” floating point layout divides the 64 available bits into distinct components, including a sign-bit, eleven exponent bits, and 52 bits for the significand.

The bit layout shows where a double-precision Number spends its 64 bits: one sign bit, eleven exponent bits, and 52 significand bits. section 3.1.1

Precision also limits integers. Number has 53 bits of accuracy, and its maximum safe integer is `2^53 - 1`, or `9,007,199,254,740,991`. Beyond that boundary, not every successive integer is representable. Floating point offers a wide range, but that range does not imply exactness at every magnitude. Binary and two’s-complement integer encodings help explain bits and signed arithmetic; they are not a replacement definition for the Number type. section 3.5 section 3.1.1

Choose the comparison or numeric type to fit the failure mode:

  • For floating-point results expected to be close, compare their difference against a suitable tolerance rather than assuming strict equality. `Number.EPSILON` can inform that tolerance; a scale-aware comparison multiplies it by the larger absolute operand. section 3.1.1
  • Use BigInt when integer values exceed Number’s safe range and exact integer representation matters. Arithmetic cannot mix BigInt and Number without an explicit conversion. section 3.5
  • Do not expect Number to provide exact decimal arithmetic. Decimal is a proposed base-10 numeric type, not an assumed available replacement. section 3.5

The specification defines Number’s behavior, not a requirement that every runtime store every value identically. V8 optimizes small integers as SMIs, avoiding heap allocation within its stated range of `-2^31` to `2^31 - 1`. Keeping numeric types consistent in arrays and loops helps preserve such optimizations; mixing types can force slower generic handling. This is a V8 implementation detail, not a new JavaScript numeric type. section 3.4 section 3.5

How JavaScript in Depth covers it

How deep it goes
Works from binary encoding through floating-point layout, precision limits, and V8 optimization, without attempting the full IEEE 754 standard.
Examples
Uses `0.1 + 0.2`, binary fraction layouts, tolerance functions, and integers around the safe-precision boundary.
What it assumes
Builds binary place values from bits and bytes while assuming familiarity with basic JavaScript arithmetic and equality checks.
What you can do after
Diagnose/debug with it when arithmetic fails exact comparisons, integers lose precision, or V8 numeric optimizations disappear.
How it connects
Connects Number to IEEE 754, Number.EPSILON, BigInt, proposed Decimal, two’s complement, and V8 SMI optimization.

Also explained in

Compare how 7 books explain number type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.