Math Object Essentials in JavaScript
Math is a namespace of constants and pure functions — not a constructor, never instantiated. Essentials: Math.round/floor/ceil/trunc (four rounding modes), Math.abs/max/min (spread for arrays: Math.max(...arr)), Math.pow/sqrt/cbrt, Math.random for [0,1) floats, and Math.trunc for toward-zero integer parts.
random() needs scaling for ranges (Math.floor(Math.random() * 6) + 1 for dice) and is NOT cryptographic — crypto.getRandomValues serves security needs. Constants Math.PI/E round out geometry and logarithms (Math.log is natural log).
Four roundings
round (nearest, halves up), floor (down), ceil (up), trunc (toward zero) — distinct tools, not synonyms.
max/min with spread
Math.max(...arr) spreads arrays into arguments — empty arrays give -Infinity/Infinity, guard them.
Scaled randoms
Math.floor(Math.random() * n) yields 0..n-1 uniformly — the dice-roll formula to memorize.
Not for secrets
Math.random is predictable — tokens, salts, and draws use crypto.getRandomValues instead.
console.log(Math.floor(3.9), Math.ceil(3.1));console.log(Math.max(3, 9, 4));console.log(Math.floor(Math.random() * 6) + 1 >= 1);Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.