SyntaxStudy
Sign Up
JavaScript Number Basics: Integers, Floats, NaN, Infinity
JavaScript Beginner 6 min read

Number Basics: Integers, Floats, NaN, Infinity

JavaScript Number Basics

JavaScript has a single numeric type — Number — which represents all numeric values as 64-bit double-precision floating-point numbers following the IEEE 754 standard. This means integers and decimals share the same type, but it also introduces some surprising behaviours when precision matters.

Integer and Float Values

There is no separate integer type. Writing 42 and 42.0 produces the same value. Integers are represented exactly up to 2^53 − 1 (also available as Number.MAX_SAFE_INTEGER). Beyond that limit, arithmetic can lose precision.

NaN — Not a Number

NaN is a special numeric value that results from invalid arithmetic, such as dividing zero by zero or parsing a non-numeric string. Crucially, NaN !== NaN — it is the only value in JavaScript not equal to itself. Use Number.isNaN() (not the global isNaN) to test for it reliably.

Infinity

Infinity and -Infinity result from operations like dividing a non-zero number by zero. They propagate through arithmetic and can be tested with Number.isFinite().

Example
console.log(typeof 42);        // number
console.log(typeof 3.14);      // number
console.log(0.1 + 0.2);        // 0.30000000000000004
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(NaN === NaN);      // false
console.log(Number.isNaN(NaN));         // true
console.log(Number.isNaN('hello'));     // false  (unlike global isNaN)
console.log(1 / 0);            // Infinity
console.log(-1 / 0);           // -Infinity
console.log(Number.isFinite(Infinity)); // false
console.log(Number.isFinite(42));       // true
Pro Tip

Always use Number.isNaN() instead of the global isNaN() — the global version coerces its argument to a number first, leading to false positives like isNaN('hello') returning true.