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>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.
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>Exercise: JavaScript Numbers
What does typeof NaN return?