TExcelTAD
Home › JavaScript › JS Numbers

JavaScript Numbers

JavaScript has a single number type for integers and decimals alike. Every number is stored as a 64-bit IEEE 754 floating-point value — the same "double" type that most other languages offer.

js · try it yourself
const whole = 42;
const decimal = 3.75;
const negative = -12;
const big = 2.5e6;       // scientific notation: 2.5 × 10⁶
const small = 4e-3;      // 0.004
const readable = 1_000_000; // underscores are just for readability
console.log(whole, decimal, negative, big, small, readable);
42 3.75 -12 2500000 0.004 1000000

Integer precision

Integers are exact up to Number.MAX_SAFE_INTEGER (2⁵³ − 1, about 9 quadrillion). Beyond that, gaps appear between representable numbers:

js · try it yourself
console.log(Number.MAX_SAFE_INTEGER);
console.log(9007199254740992 + 1);         // can't represent ...993
console.log(Number.isSafeInteger(2 ** 53));
console.log(999999999999999999);           // 18 nines
9007199254740991
9007199254740992
false
1000000000000000000

For bigger exact integers, use BigInt (next chapters).

Decimal precision

Decimal fractions like 0.1 can't be stored exactly in binary, so small rounding errors creep in. Compare decimals with a tolerance, or round before displaying:

js · try it yourself
const sum = 0.1 + 0.2;
console.log(sum);
console.log(sum === 0.3);
console.log(Math.abs(sum - 0.3) < Number.EPSILON);
console.log(sum.toFixed(2));
// Money tip: count in cents
const priceCents = 1999;
console.log((priceCents * 3) / 100);
0.30000000000000004
false
true
0.30
59.97

Numbers and strings

+ with a string concatenates, but the other arithmetic operators try to convert strings to numbers:

js · try it yourself
console.log(10 + 20);
console.log("10" + 20);
console.log(10 + 20 + "30");   // 30 + "30"
console.log("100" / "4");
console.log("100" * 2, "100" - 1);
console.log("abc" * 2);
30
1020
3030
25
200 99
NaN

NaN — Not a Number

NaN is the result of a failed numeric operation. Oddly, it's of type number, and it's the only value that isn't equal to itself — so check for it with Number.isNaN():

js · try it yourself
const bad = Number("twelve");
console.log(bad);
console.log(typeof bad);
console.log(bad === NaN);           // never true!
console.log(Number.isNaN(bad));
console.log(bad + 5);               // NaN spreads through calculations
NaN
number
false
true
NaN

Infinity

Dividing by zero, or exceeding the largest number (about 1.8 × 10³⁰⁸), gives Infinity or -Infinity:

js · try it yourself
console.log(1 / 0, -1 / 0);
console.log(Number.MAX_VALUE * 2);
console.log(Number.isFinite(1 / 0), Number.isFinite(42));
Infinity -Infinity
Infinity
false true

Other bases

Numbers can be written in hexadecimal (0x), binary (0b) or octal (0o). toString(base) converts the other way:

Literal Base Value
0xff 16 255
0b1010 2 10
0o17 8 15
js
(255).toString(16);  // "ff"
(10).toString(2);    // "1010"

Note: Avoid new Number(5) — like new String, it creates a wrapper object, not a number, and breaks === comparisons.

Test yourself

What is "5" * "4"?

  1. "54"
  2. 20
  3. NaN
  4. "20"

How should you test whether x is NaN?

  1. x === NaN
  2. x == NaN
  3. Number.isNaN(x)
  4. typeof x === "NaN"

What is the result of 7 / 0?

  1. 0
  2. NaN
  3. Infinity
  4. An error