Number

1. Introduction: The Unified Number Type

In JavaScript, there is only one type for numbers: number. This single type is used to represent both integers (whole numbers like 10, -50) and floating-point numbers (decimals like 3.14, -0.5).

The Core Standard: All numbers in JavaScript are implemented as 64-bit double-precision floating-point numbers, following the international IEEE 754 standard. This is the same format used for double in languages like C++ and Java.

let integer = 100;
let float = 99.5;

console.log(typeof integer); // "number"
console.log(typeof float);   // "number"


2. Creating Numbers

You can create numbers in several ways:


3. The "Gotcha": Floating-Point Inaccuracy

Because numbers are stored in a binary floating-point format (base-2), they cannot perfectly represent all decimal fractions (base-10). This leads to the most famous "gotcha" in JavaScript math.

console.log(0.1 + 0.2); // Outputs: 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false

Why? The numbers 0.1 and 0.2 (and 0.3) have infinitely repeating representations in binary, similar to how 1/3 is 0.333... in decimal. The computer has to round them off to fit them into the 64-bit storage. These tiny rounding errors accumulate.

How to handle this:

  1. For financial calculations: Never use floating-point numbers. Work with integers (e.g., store money in cents).
  2. For display: Use the .toFixed() method to round the result to a specific number of decimal places.