JavaScript Math Object
The Math object provides properties and methods for mathematical constants and functions. Unlike other objects, Math is not a constructor -- you access all properties and methods directly as Math.property or Math.method().
Rounding Methods
JavaScript provides four main rounding methods: round(), ceil(), floor(), and trunc(). Each behaves differently with positive and negative numbers.
const num = 4.7;
const neg = -4.3;
// round() - nearest integer
console.log("round(4.7):", Math.round(num));
console.log("round(4.4):", Math.round(4.4));
console.log("round(-4.3):", Math.round(neg));
// ceil() - round UP
console.log("ceil(4.1):", Math.ceil(4.1));
console.log("ceil(-4.1):", Math.ceil(-4.1));
// floor() - round DOWN
console.log("floor(4.9):", Math.floor(4.9));
console.log("floor(-4.1):", Math.floor(-4.1));
// trunc() - remove decimals (toward zero)
console.log("trunc(4.9):", Math.trunc(4.9));
console.log("trunc(-4.9):", Math.trunc(-4.9));| Method | 4.3 | 4.7 | -4.3 | -4.7 |
|---|---|---|---|---|
| round() | 4 | 5 | -4 | -5 |
| ceil() | 5 | 5 | -4 | -4 |
| floor() | 4 | 4 | -5 | -5 |
| trunc() | 4 | 4 | -4 | -4 |
Math.abs() and Math.sign()
Math.abs() returns the absolute (positive) value of a number. Math.sign() returns -1, 0, or 1 indicating the sign of the number.
// abs() - absolute value
console.log("abs(-7.5):", Math.abs(-7.5));
console.log("abs(7.5):", Math.abs(7.5));
console.log("abs(0):", Math.abs(0));
// sign() - returns -1, 0, or 1
console.log("sign(-42):", Math.sign(-42));
console.log("sign(0):", Math.sign(0));
console.log("sign(42):", Math.sign(42));
// Practical: calculate difference
const a = 10, b = 25;
console.log("Difference:", Math.abs(a - b));Math.pow() and Math.sqrt()
Math.pow(base, exponent) raises a number to a power. Math.sqrt() returns the square root. You can also use the ** operator instead of Math.pow().
// pow(base, exponent)
console.log("2^3:", Math.pow(2, 3));
console.log("5^2:", Math.pow(5, 2));
console.log("2^10:", Math.pow(2, 10));
// ** operator (same as pow)
console.log("2**3:", 2 ** 3);
// sqrt()
console.log("sqrt(16):", Math.sqrt(16));
console.log("sqrt(2):", Math.sqrt(2));
// cbrt() - cube root
console.log("cbrt(27):", Math.cbrt(27));
// Hypotenuse
console.log("hypot(3,4):", Math.hypot(3, 4));Math.min() and Math.max()
Math.min() and Math.max() return the smallest and largest of the given numbers. Use the spread operator to pass an array of values.
// Direct values
console.log("min:", Math.min(5, 2, 9, 1, 7));
console.log("max:", Math.max(5, 2, 9, 1, 7));
// With array (use spread)
const numbers = [45, 12, 78, 3, 99, 23];
console.log("Array min:", Math.min(...numbers));
console.log("Array max:", Math.max(...numbers));
// No arguments
console.log("min():", Math.min()); // Infinity
console.log("max():", Math.max()); // -Infinity
// Clamp a value between min and max
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
console.log("clamp(150, 0, 100):", clamp(150, 0, 100));
console.log("clamp(-10, 0, 100):", clamp(-10, 0, 100));
console.log("clamp(50, 0, 100):", clamp(50, 0, 100));Math Constants and Logarithms
Math provides several mathematical constants like PI and E, along with logarithmic functions.
// Constants
console.log("PI:", Math.PI);
console.log("E:", Math.E);
console.log("SQRT2:", Math.SQRT2);
console.log("LN2:", Math.LN2);
console.log("LN10:", Math.LN10);
// Logarithms
console.log("log(1):", Math.log(1)); // natural log (base e)
console.log("log(E):", Math.log(Math.E));
console.log("log2(8):", Math.log2(8)); // base 2
console.log("log10(1000):", Math.log10(1000)); // base 10
// Circle area using PI
const radius = 5;
const area = Math.PI * Math.pow(radius, 2);
console.log("Circle area (r=5):", area.toFixed(2));