Home/Concepts/Math Functions
🔢
Beginner⏱ 8 min#03 / 08

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.

Your Progress0/8 complete
0 visited0%
💡 Real-World Analogy

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 Constants

Math.PI = 3.14159..., Math.E = 2.718..., Math.SQRT2 = 1.414...

Rounding

Math.round (nearest), Math.floor (down), Math.ceil (up), Math.trunc (drop decimal).

Power, Root, Random

Math.pow, Math.sqrt, Math.cbrt, Math.abs, Math.max, Math.min, Math.random.

💻 Code Examples

1Constants & Rounding
math.js
1// Constants
2Math.PI; // 3.141592653589793
3Math.E; // 2.718281828459045
4 
5// 4 ways to round
6Math.round(4.5); // 5 (nearest integer)
7Math.round(4.4); // 4
8 
9Math.floor(4.9); // 4 (always down)
10Math.ceil(4.1); // 5 (always up)
11 
12Math.trunc(4.9); // 4 (just drops decimal)
13Math.trunc(-4.9); // -4 (toward zero)
14 
15// Key difference
16Math.floor(-4.1); // -5 (more negative)
17Math.trunc(-4.1); // -4 (toward zero)
2Power, Root & Absolute
math.js
1// Powers
2Math.pow(2, 10); // 1024
32 ** 10; // 1024 (same, newer syntax)
4 
5// Roots
6Math.sqrt(144); // 12 (square root)
7Math.cbrt(27); // 3 (cube root)
8 
9// Absolute value
10Math.abs(-42); // 42 (removes sign)
11Math.abs(42); // 42
12 
13// Max & Min
14Math.max(10, 25, 3, 99, 7); // 99
15Math.min(10, 25, 3, 99, 7); // 3
16 
17// With arrays using spread
18let scores = [85, 92, 78, 95, 88];
19Math.max(...scores); // 95
20Math.min(...scores); // 78
3Random Numbers
math.js
1// Math.random() returns [0, 1)
2Math.random(); // 0.7234...
3 
4// Random integer 0 to 9
5Math.floor(Math.random() * 10);
6 
7// Random integer 1 to 10
8Math.floor(Math.random() * 10) + 1;
9 
10// Random between min and max (inclusive)
11function randInt(min, max) {
12 return Math.floor(Math.random() * (max - min + 1)) + min;
13}
14randInt(1, 6); // dice roll: 1-6
15randInt(50, 100); // range 50-100
16 
17// Round to decimal places
18let pi = Math.PI;
19Math.round(pi * 100) / 100; // 3.14
20pi.toFixed(2); // "3.14" (string)

⚠️ Common Mistakes

❌ Mistake

Math.floor vs Math.round confusion

✅ Fix

floor ALWAYS goes down (4.9 → 4). round goes to nearest (4.5 → 5, 4.4 → 4).

❌ Mistake

Thinking Math.random() can return 1

✅ Fix

Math.random() returns 0 to 0.9999... - it NEVER reaches 1.

❌ Mistake

Wrong casing - math() or Math.Round()

✅ Fix

It is always Math (capital M) with lowercase method names: Math.round(), Math.floor().

📋 Quick Reference

Math.PIMath.round(n)Math.floor(n)Math.ceil(n)Math.abs(n)Math.sqrt(n)Math.pow(base, exp)Math.max(...arr)Math.random()

Live Code Playground

Edit the code and press ▶ Run to see output instantly.

math_playground.js
⟳ Loading editor...
💾 Auto-saved
// Press ▶ Run Code to execute and view output console logs...
🔤
← Previous
Strings
Challenge Mode
Next →
Loops
🔄