JavaScript Numbers

JavaScript has a single number type that stores integers and decimals as 64-bit floating point values.

JavaScript Numbers

Unlike many languages, JavaScript does not separate integers from decimals. Every number — whether 7, -3, or 3.14 — is stored in the same format: a 64-bit double-precision floating point value. This keeps the language simple, but it also introduces a few rounding quirks worth knowing about.

Writing numbers

<!DOCTYPE html>
<html>
<body>

<h2>Writing numbers</h2>

<script>
let integer = 42;
let decimal = 3.14;
let negative = -7;
let large = 1e6;   // 1000000 in exponential notation
let small = 2e-3;  // 0.002

console.log(integer, decimal, negative, large, small);
</script>

</body>
</html>

The standard arithmetic operators work on numbers as you would expect. Note that dividing by zero does not crash the program — it produces the special value Infinity instead.

Arithmetic and special values

<!DOCTYPE html>
<html>
<body>

<h2>Arithmetic and special values</h2>

<script>
console.log(10 + 5);  // 15
console.log(10 / 3);  // 3.3333333333333335
console.log(2 ** 10); // 1024 (exponentiation)

console.log(5 / 0);       // Infinity
console.log(-5 / 0);      // -Infinity
console.log("abc" * 2);   // NaN (Not a Number)
</script>

</body>
</html>
Note: Because of floating point storage, 0.1 + 0.2 does not equal exactly 0.3 — it gives 0.30000000000000004. For money and precise comparisons, round the result or work in whole units such as cents.

JavaScript defines a few special numeric values you will meet often. NaN stands for "Not a Number" and appears when a math operation has no meaningful result. Infinity represents a value larger than any other number.

  • NaN — the result of an invalid calculation such as multiplying text by a number.
  • Infinity and -Infinity — results that overflow the representable range, or division by zero.
  • Number.MAX_SAFE_INTEGER — the largest integer that can be represented exactly (9007199254740991).
  • BigInt — a separate type, written like 123n, for integers beyond the safe range.
Note: NaN is famously not equal to itself, so NaN === NaN is false. To test for it reliably, use Number.isNaN(value).

Detecting NaN

<!DOCTYPE html>
<html>
<body>

<h2>Detecting NaN</h2>

<script>
let result = Number("hello");

console.log(result);              // NaN
console.log(result === NaN);      // false (never use this)
console.log(Number.isNaN(result)); // true (correct check)
</script>

</body>
</html>
Value / ConceptMeaningExample
IntegerWhole number42
FloatNumber with a decimal part3.14
ExponentialShorthand for very large or small numbers1e6 → 1000000
NaNNot a Number — invalid math result"abc" * 2
InfinityLarger than any number1 / 0
MAX_SAFE_INTEGERLargest exact integer9007199254740991

Exercise: JavaScript Numbers

What does typeof NaN return?