Math Functions
Numbers & Calculations
The JavaScript Math object is a built-in namespace with mathematical constants and functions for rounding, roots, powers, random numbers, and more.
The Math object is like a scientific calculator baked into JavaScript. It has special keys for rounding (floor/ceil/round), a randomizer button, and even knows the value of pi.
🔑 Key Concepts
Math.PI = 3.14159..., Math.E = 2.718..., Math.SQRT2 = 1.414...
Math.round (nearest), Math.floor (down), Math.ceil (up), Math.trunc (drop decimal).
Math.pow, Math.sqrt, Math.cbrt, Math.abs, Math.max, Math.min, Math.random.
💻 Code Examples
⚠️ Common Mistakes
Math.floor vs Math.round confusion
floor ALWAYS goes down (4.9 → 4). round goes to nearest (4.5 → 5, 4.4 → 4).
Thinking Math.random() can return 1
Math.random() returns 0 to 0.9999... - it NEVER reaches 1.
Wrong casing - math() or Math.Round()
It is always Math (capital M) with lowercase method names: Math.round(), Math.floor().
📋 Quick Reference
Live Code Playground
Edit the code and press ▶ Run to see output instantly.