R
Rishtaara
JavaScript Fundamentals
Lesson 7 of 28Article15 min

Numbers, NaN, Infinity & Math Object

All numbers in JS are stored as floating point — integers like 42 and decimals like 3.14 use the same type.

JavaScript Numbers

All numbers in JS are stored as floating point — integers like 42 and decimals like 3.14 use the same type.

Real-life example: A weighing scale shows whole kg and grams on one display — one number line, many formats.

Integers and floats
const whole = 100;
const price = 99.99;
const big = 1e6; // 1000000

NaN and Infinity

NaN means Not a Number — bad math like "hello" * 2. Infinity is larger than any number — 1 / 0 gives Infinity.

Real-life example: NaN is asking "what is apple plus Tuesday?" — no valid answer. Infinity is a road with no end sign.

NaN and Infinity
"text" * 2;     // NaN
Number.isNaN(NaN); // true
1 / 0;          // Infinity
-1 / 0;         // -Infinity

Math.round, floor, ceil

Math.round goes to nearest integer. Math.floor rounds down. Math.ceil rounds up.

Real-life example: round — nearest rupee. floor — how many full boxes fit. ceil — minimum boxes to ship all items.

Rounding
Math.round(4.6);  // 5
Math.floor(4.9);  // 4
Math.ceil(4.1);   // 5

Math.random, max, min, abs

Math.random() gives 0 to less than 1. Math.max and Math.min pick largest/smallest. Math.abs removes minus sign.

Real-life example: random — rolling a dice. max/min — tallest and shortest student. abs — distance (always positive).

For money on Rishtaara-style apps, store paise as integers (4999 = ₹49.99) to avoid float rounding bugs.
Math utilities
Math.random();              // e.g. 0.734...
Math.floor(Math.random() * 6) + 1; // dice 1–6
Math.max(10, 25, 3);         // 25
Math.min(10, 25, 3);         // 3
Math.abs(-7);                // 7