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.
const whole = 100;
const price = 99.99;
const big = 1e6; // 1000000NaN 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.
"text" * 2; // NaN
Number.isNaN(NaN); // true
1 / 0; // Infinity
-1 / 0; // -InfinityMath.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.
Math.round(4.6); // 5
Math.floor(4.9); // 4
Math.ceil(4.1); // 5Math.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).
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