Chapter III · Data Types

Numbers

JavaScript uses the Number type for all numeric values, employing 64-bit double-precision floating-point format.

01 What are Numbers?

Unlike many programming languages that separate integers from decimals, JavaScript has only one Number type. Numbers are represented as 64-bit floating-point values (IEEE 754 standard).

This format can safely represent integers between -(2^53 - 1) and 2^53 - 1. It also includes special numeric symbols like Infinity, -Infinity, and NaN (Not-a-Number).

02 Simple Example

Performing arithmetic and encountering IEEE 754 floating-point precision quirks.

numbers.js
let x = 0.1 + 0.2;
console.log(x); // 0.30000000000000004
console.log(x === 0.3); // false

Due to binary floating-point representation limits, decimal arithmetic can occasionally yield minor rounding inaccuracies.

03 How It Works

Numbers are stored directly as primitive values. The global Math object and Number constructor provide utilities for rounding, parsing, and bounds checking.

Variable Reference Primitive Value

Numbers store floating-point numeric data directly as fast primitive values.

04 Step-by-Step

  1. Numeric literals are parsed and stored as 64-bit IEEE 754 binary floating points.

  2. Arithmetic operators compute mathematical results.

  3. Special values like NaN are returned when mathematical operations result in undefined numerical states.

05 Mental Model

"A digital calculator display operating with fixed precision slots."

It handles huge ranges of integers and decimals smoothly, with rare rounding quirks at microscopic decimal levels.

06 Interview Questions

Q: Why does 0.1 + 0.2 !== 0.3 in JavaScript?

Numbers are IEEE 754 doubles — a binary format that can't represent most decimal fractions exactly, producing tiny rounding errors.

Q: What is Number.MAX_SAFE_INTEGER and why does it matter?

2^53 - 1 — the largest integer that can be represented without precision loss. Beyond it, integer math can silently become inaccurate, which is what BigInt exists to solve.

Key Takeaway

JavaScript numbers are 64-bit floating-point values handling both integers and decimals, equipped with special values like NaN and Infinity.

Related Concepts