JavaScript Course Lesson 6 of 16
The Codex / JavaScript / Numbers & Math

Numbers & Math

JavaScript's single number type, floating-point surprises, NaN, Infinity, the Math object, and BigInt.

What it is, in plain English: JavaScript has one number type for everything — whole numbers, decimals, negative numbers. Unlike Python, there's no separate int and float. All numbers are stored as 64-bit floating-point (IEEE 754), which means very large or very precise decimal numbers can have tiny rounding errors. NaN (Not a Number) and Infinity are special number values, not errors.

Numbers in JavaScript

JavaScript keeps things simple: there is exactly one number type. Whether you write 42 or 3.14 or -100, they're all the same type. No separate integers and floats like Python.

Basic arithmetic

console.log(5 + 3);    // 8    — addition
console.log(10 - 4);   // 6    — subtraction
console.log(6 * 7);    // 42   — multiplication
console.log(15 / 4);   // 3.75 — division (always gives decimal if needed)
console.log(15 % 4);   // 3    — remainder (what's left after dividing)
console.log(2 ** 8);   // 256  — exponent (2 to the power of 8)

The remainder operator % is worth knowing — it's how you check if a number is even (n % 2 === 0) or fits into a time period (seconds % 60 gives the leftover seconds).

The floating-point surprise

Here's something that surprises almost every beginner:

console.log(0.1 + 0.2);   // 0.30000000000000004

That's not a bug in your code. It's how all computers store decimal numbers — they use binary, and 0.1 can't be represented exactly in binary (like 1/3 can't be written exactly in decimal). The tiny rounding error is fundamental to how computers work.

The fix for display purposes:

let total = 0.1 + 0.2;
console.log(total.toFixed(2));   // "0.30" — rounds to 2 decimal places, returns a string
console.log(+total.toFixed(2));  // 0.3    — the + converts it back to a number

NaN — Not a Number

NaN is what JavaScript returns when a mathematical operation doesn't produce a valid number:

console.log("hello" * 2);    // NaN — can't multiply text
console.log(0 / 0);          // NaN
console.log(Math.sqrt(-1));   // NaN — no real square root of negative

// NaN is type "number" — which is confusing but true:
console.log(typeof NaN);     // "number"

// NaN is the only value not equal to itself:
console.log(NaN === NaN);    // false!

// Safe way to check for NaN:
console.log(Number.isNaN(NaN));        // true
console.log(Number.isNaN("hello"));    // false (it's a string, not NaN)

Infinity

console.log(1 / 0);           // Infinity
console.log(-1 / 0);          // -Infinity
console.log(Infinity + 1);    // Infinity
console.log(Number.isFinite(Infinity));  // false
console.log(Number.isFinite(42));        // true

The Math object — useful maths functions

Math is a built-in object with mathematical tools. You don't need to import anything — it's always available:

Math.round(4.5)    // 5  — round to nearest integer
Math.floor(4.9)    // 4  — always round down
Math.ceil(4.1)     // 5  — always round up
Math.abs(-7)       // 7  — absolute value (remove the minus sign)
Math.max(3, 7, 2)  // 7  — largest value
Math.min(3, 7, 2)  // 2  — smallest value
Math.pow(2, 10)    // 1024 — same as 2 ** 10
Math.sqrt(16)      // 4  — square root
Math.PI            // 3.141592653589793

// Random numbers:
Math.random()               // random decimal 0.0 to 0.9999...
Math.floor(Math.random() * 6) + 1  // random integer 1 to 6 (like a dice)

Converting strings to numbers

// User input always comes as a string — convert before calculating:
let userInput = "42";
console.log(userInput + 10);      // "4210" — string concatenation! Wrong.
console.log(Number(userInput) + 10);  // 52   — converted first

// parseInt and parseFloat:
parseInt("42px")      // 42  — stops at the first non-number character
parseFloat("3.14em")  // 3.14
parseInt("hello")     // NaN — no number to extract
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.