number

Contents

number#

A single number type, IEEE 754 double. Number.MAX_SAFE_INTEGER (2^53 - 1) is the largest integer-representable value; beyond that, reach for bigint.

typeof on a number literal.

typeof 42;            // "number"

Division by zero is not an error; it produces Infinity.

1 / 0;                // Infinity

0 / 0 is the canonical NaN.

0 / 0;                // NaN

Number.isFinite does not coerce; Number.isNaN is the correct NaN test.

Number.isFinite(1);   // true
Number.isNaN(NaN);    // true

parseInt reads a leading integer with a base; pass the base explicitly.

parseInt("ff", 16);   // 255

Number.parseFloat reads the leading float and stops.

Number.parseFloat("1.5km");  // 1.5

NaN !== NaN and 0.1 + 0.2 !== 0.3 are the two recurring sources of bugs. Compare floats with a tolerance, and test for NaN with Number.isNaN.

References#

  • bigint for arbitrary-precision integers.

  • Casting for Number(s) vs parseInt / parseFloat.