Skip to content

JavaScript Tutorial Numbers and Math

JavaScript Numbers

Lesson 35 of 258 2 min read By Deepak Paswan Updated Oct 4, 2026

JavaScript has one main number type for both whole numbers and decimals.

script.js
JS
const whole = 42;
const decimal = 3.14;
const negative = -7;
const big = 1_000_000;   // underscores make big numbers readable
const exp = 2e3;         // 2 × 10³

console.log(whole, decimal, negative, big, exp);
console.log(typeof whole, typeof decimal); // → number number

NaN

NaN means “Not a Number”. You get it when a calculation makes no sense.

script.js
JS
console.log("hello" * 2);       // → NaN
console.log(Number("abc"));     // → NaN
console.log(0 / 0);             // → NaN
console.log(typeof NaN);        // → number

NaN is the only value that is not equal to itself, so NaN === NaN is false. Check for it with Number.isNaN().

script.js
JS
console.log(Number.isNaN(NaN));      // → true
console.log(Number.isNaN("hello"));  // → false
console.log(isNaN("hello"));         // → true  (old global version converts first)

Prefer Number.isNaN(). The old global isNaN() converts its argument to a number first, so text gives surprising results.

Infinity

script.js
JS
console.log(1 / 0);                       // → Infinity
console.log(-1 / 0);                      // → -Infinity
console.log(Number.isFinite(100));        // → true
console.log(Number.isFinite(Infinity));   // → false
console.log(Number.isFinite("100"));      // → false

Useful number methods

script.js
JS
const price = 19.987;

console.log(price.toFixed(2));        // → 19.99
console.log(Number.isInteger(5));     // → true
console.log(Number.isInteger(5.5));   // → false
console.log((255).toString(16));      // → ff
console.log((1234567.891).toLocaleString("en-US")); // → 1,234,567.891

toFixed() returns a string, not a number, so do your maths first and format last.

Safe ranges

Integers are exact up to Number.MAX_SAFE_INTEGER (9007199254740991). Beyond that, use BigInt.

lightbulb
Format numbers for people
toLocaleString() and Intl.NumberFormat format numbers, currencies and percentages for a language and region, so you do not have to build commas and symbols by hand.