Numbers, Math & Date
Number Methods
const n = 123.456;
n.toFixed(2); // "123.46" (string, rounded)
n.toPrecision(5); // "123.46" (5 significant digits)
n.toString(); // "123.456"
n.toString(16); // hex string
n.toString(2); // binary string
// Number static methods
Number.isInteger(5); // true
Number.isInteger(5.5); // false
Number.isFinite(Infinity); // false
Number.isFinite(5); // true
Number.isNaN(NaN); // true
Number.isNaN("abc"); // false (strict!)
Number.parseInt("42.9"); // 42
Number.parseFloat("42.9"); // 42.9
// Safe integer range
Number.MAX_SAFE_INTEGER; // 9007199254740991 (2^53 - 1)
Number.MIN_SAFE_INTEGER; // -9007199254740991
Number.isSafeInteger(9007199254740991); // true
Number.isSafeInteger(9007199254740992); // false
// Special values
Number.MAX_VALUE;
Number.MIN_VALUE;
Number.POSITIVE_INFINITY;
Number.NEGATIVE_INFINITY;
Number.EPSILON; // smallest float difference
// Floating point gotcha!
0.1 + 0.2 === 0.3; // false (floating point precision!)
(0.1 + 0.2).toFixed(1) === "0.3"; // true (workaround)
Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON; // true (proper comparison)
Math Object
Math.PI; // 3.141592653589793
Math.E; // 2.718281828459045
Math.SQRT2; // 1.414...
// Rounding
Math.round(4.5); // 5 (rounds .5 up)
Math.round(4.4); // 4
Math.floor(4.9); // 4 (always down)
Math.ceil(4.1); // 5 (always up)
Math.trunc(4.9); // 4 (remove decimal, toward zero)
Math.trunc(-4.9); // -4
// Min / Max
Math.max(1, 5, 3); // 5
Math.min(1, 5, 3); // 1
Math.max(...[1, 5, 3]); // 5 (spread array)
// Powers and roots
Math.pow(2, 10); // 1024 (same as 2**10)
Math.sqrt(16); // 4
Math.cbrt(27); // 3 (cube root)
Math.hypot(3, 4); // 5 (Pythagorean theorem)
// Logarithms
Math.log(Math.E); // 1 (natural log)
Math.log2(8); // 3 (base 2)
Math.log10(100); // 2 (base 10)
// Trig
Math.sin(Math.PI / 2); // 1
Math.cos(0); // 1
// Random
Math.random(); // 0 <= x < 1
Math.floor(Math.random() * 6) + 1; // random int 1-6 (dice)
// Random integer between min and max (inclusive)
const randInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
Math.abs(-5); // 5 (absolute value)
Math.sign(-3); // -1 (negative)
Math.sign(0); // 0
Math.sign(3); // 1 (positive)
Date Object
// Creating dates
const now = new Date(); // current date/time
const d1 = new Date(2024, 0, 15); // Jan 15, 2024 (months are 0-indexed!)
const d2 = new Date("2024-03-25"); // from ISO string
const d3 = new Date(1711324800000); // from Unix timestamp (ms)
// Getting parts
const d = new Date();
d.getFullYear(); // 4-digit year
d.getMonth(); // 0-11 (0 = January!)
d.getDate(); // 1-31 (day of month)
d.getDay(); // 0-6 (0 = Sunday)
d.getHours(); // 0-23
d.getMinutes(); // 0-59
d.getSeconds(); // 0-59
d.getTime(); // Unix timestamp in ms
// Setting parts
d.setFullYear(2025);
d.setMonth(11); // December
d.setDate(31);
// Formatting
d.toISOString(); // "2024-03-25T10:30:00.000Z"
d.toLocaleDateString(); // "3/25/2024"
d.toLocaleString("en-IN"); // Indian locale format
// Better formatting with Intl
new Intl.DateTimeFormat("en-US", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric"
}).format(new Date()); // "Wednesday, March 25, 2026"
// Date arithmetic
const tomorrow = new Date(Date.now() + 86400000);
const diffMs = new Date("2025-01-01") - new Date();
const diffDays = Math.floor(diffMs / 86400000);
// Performance timing
const start = Date.now();
// ... some code ...
const elapsed = Date.now() - start; // ms elapsed
MCQ — Numbers, Math & Date
Q1: What is 0.1 + 0.2 === 0.3 in JavaScript?
A) true
B) false ✅
C) Depends on the JS engine
D) NaN
Answer: B — Floating point precision issue. 0.1 + 0.2 = 0.30000000000000004.
Q2: What does new Date().getMonth() return for January?
A) 1
B) 0 ✅
C) "January"
D) "Jan"
Answer: B — Months are 0-indexed: 0 = January, 11 = December.
Q3: What does Math.trunc(-4.7) return?
A) -5
B) -4 ✅
C) 4
D) 5
Answer: B — trunc removes the decimal toward zero. -4.7 becomes -4.
Q4: What is Number.MAX_SAFE_INTEGER?
A) The maximum number JS can represent B) 2^53 - 1 = 9007199254740991 ✅ C) 2^32 - 1 D) Infinity
Answer: B — Integers above this can lose precision. Use BigInt for larger values.
Q5: How do you generate a random integer from 1 to 10?
A) Math.random() * 10
B) Math.round(Math.random() * 10)
C) Math.floor(Math.random() * 10) + 1 ✅
D) parseInt(Math.random() * 10)
Answer: C — Math.floor(Math.random() * 10) gives 0-9, +1 shifts to 1-10.