14.2 Math Object
Overview
The Math object in JavaScript provides a collection of properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructorβyou use its methods directly.
Math Object Structure
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Math Object β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Not a constructor - cannot use 'new Math()' β
β All properties and methods are static β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββ βββββββββββββββββββββββββββββββββββββββ β
β β Constants β β Methods β β
β βββββββββββββββββββ€ βββββββββββββββββββββββββββββββββββββββ€ β
β β Math.PI β β Rounding: round, floor, ceil, trunc β β
β β Math.E β β Min/Max: min, max β β
β β Math.LN2 β β Random: random β β
β β Math.LN10 β β Power: pow, sqrt, cbrt, hypot β β
β β Math.LOG2E β β Trig: sin, cos, tan, asin, etc. β β
β β Math.LOG10E β β Log: log, log2, log10 β β
β β Math.SQRT2 β β Other: abs, sign, exp, expm1 β β
β β Math.SQRT1_2 β β β β
β βββββββββββββββββββ βββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Mathematical Constants
| Constant | Approximate Value | Description |
|---|
Math.PI | 3.14159... | Ratio of circumference to diameter |
Math.E | 2.71828... | Euler's number (natural log base) |
Math.LN2 | 0.69314... | Natural logarithm of 2 |
Math.LN10 | 2.30258... | Natural logarithm of 10 |
Math.LOG2E | 1.44269... | Base-2 logarithm of E |
Math.LOG10E | 0.43429... | Base-10 logarithm of E |
Math.SQRT2 | 1.41421... | Square root of 2 |
Math.SQRT1_2 | 0.70710... | Square root of 1/2 |
console.log(Math.PI);
console.log(Math.E);
console.log(Math.SQRT2);
Rounding Methods
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Rounding Methods β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Number Line: -3 -2 -1 0 1 2 3 β
β βββββββΌββββββΌββββββΌββββββΌββββββΌββββββ€ β
β β
β For 2.7: β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β floor(2.7) = 2 βββ rounds toward -β β
β ceil(2.7) = 3 βββ rounds toward +β β
β round(2.7) = 3 βββ rounds to nearest (0.5 rounds up) β
β trunc(2.7) = 2 βββ removes decimal part β
β β
β For -2.7: β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β floor(-2.7) = -3 βββ rounds toward -β β
β ceil(-2.7) = -2 βββ rounds toward +β β
β round(-2.7) = -3 βββ rounds to nearest β
β trunc(-2.7) = -2 βββ removes decimal part β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Math.round(2.4);
Math.round(2.5);
Math.round(2.6);
Math.round(-2.5);
Math.floor(2.9);
Math.floor(-2.1);
Math.ceil(2.1);
Math.ceil(-2.9);
Math.trunc(2.9);
Math.trunc(-2.9);
Rounding to Decimal Places
function roundTo(num, decimals) {
const factor = Math.pow(10, decimals);
return Math.round(num * factor) / factor;
}
roundTo(3.14159, 2);
roundTo(3.14159, 4);
function floorTo(num, decimals) {
const factor = Math.pow(10, decimals);
return Math.floor(num * factor) / factor;
}
function ceilTo(num, decimals) {
const factor = Math.pow(10, decimals);
return Math.ceil(num * factor) / factor;
}
Min and Max
Math.min(1, 2, 3);
Math.max(1, 2, 3);
Math.min(-1, -5, 0);
Math.max(-1, -5, 0);
const numbers = [5, 2, 8, 1, 9];
Math.min(...numbers);
Math.max(...numbers);
Math.min();
Math.max();
Math.min(1, NaN, 3);
Random Numbers
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Math.random() β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Returns: 0 β€ x < 1 (0 inclusive, 1 exclusive) β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β 0 1 β
β β² β
β included not included βββββββΆ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Math.random();
function randomInt(max) {
return Math.floor(Math.random() * max);
}
randomInt(10);
function randomRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
randomRange(1, 6);
randomRange(10, 20);
function randomFloat(min, max) {
return Math.random() * (max - min) + min;
}
randomFloat(1.5, 3.5);
function randomBool() {
return Math.random() < 0.5;
}
function randomElement(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
randomElement(['a', 'b', 'c']);
function shuffle(arr) {
const result = [...arr];
for (let i = result.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[result[i], result[j]] = [result[j], result[i]];
}
return result;
}
Power and Root Methods
| Method | Description | Example |
|---|
Math.pow(x, y) | x raised to power y | Math.pow(2, 3) β 8 |
Math.sqrt(x) | Square root | Math.sqrt(16) β 4 |
Math.cbrt(x) | Cube root | Math.cbrt(27) β 3 |
Math.hypot(...values) | Hypotenuse | Math.hypot(3, 4) β 5 |
** operator | Power (ES2016) | 2 ** 3 β 8 |
Math.pow(2, 3);
Math.pow(2, 0.5);
2 ** 3;
2 ** -1;
Math.sqrt(16);
Math.sqrt(-1);
Math.cbrt(27);
Math.cbrt(-8);
Math.hypot(3, 4);
Math.hypot(5, 12);
function distance(x1, y1, x2, y2) {
return Math.hypot(x2 - x1, y2 - y1);
}
distance(0, 0, 3, 4);
Absolute Value and Sign
Math.abs(-5);
Math.abs(5);
Math.abs(-3.14);
Math.sign(-5);
Math.sign(0);
Math.sign(5);
Math.sign(-0);
Exponential and Logarithmic
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Exponential and Logarithmic Functions β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Exponential: Logarithmic: β
β βββββββββββββ ββββββββββββ β
β Math.exp(x) = eΛ£ Math.log(x) = ln(x) β
β Math.expm1(x) = eΛ£ - 1 Math.log1p(x) = ln(1 + x) β
β Math.log2(x) = logβ(x) β
β Math.log10(x) = logββ(x) β
β β
β Relationship: if y = eΛ£ then x = ln(y) β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Math.exp(1);
Math.exp(2);
Math.expm1(0);
Math.log(Math.E);
Math.log(1);
Math.log(0);
Math.log(-1);
Math.log2(8);
Math.log10(1000);
function logBase(x, base) {
return Math.log(x) / Math.log(base);
}
logBase(8, 2);
Trigonometric Functions
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Trigonometric Functions β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β NOTE: All angles are in RADIANS, not degrees! β
β β
β Conversion: β
β βββββββββββ β
β radians = degrees Γ (Ο / 180) β
β degrees = radians Γ (180 / Ο) β
β β
β Common Values: β
β ββββββββββββββ β
β 0Β° = 0 rad 90Β° = Ο/2 rad β
β 180Β° = Ο rad 360Β° = 2Ο rad β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Method | Description |
|---|
Math.sin(x) | Sine of x (radians) |
Math.cos(x) | Cosine of x |
Math.tan(x) | Tangent of x |
Math.asin(x) | Arcsine (inverse sine) |
Math.acos(x) | Arccosine |
Math.atan(x) | Arctangent |
Math.atan2(y, x) | Arctangent of y/x |
Math.sinh(x) | Hyperbolic sine |
Math.cosh(x) | Hyperbolic cosine |
Math.tanh(x) | Hyperbolic tangent |
function toRadians(degrees) {
return degrees * (Math.PI / 180);
}
function toDegrees(radians) {
return radians * (180 / Math.PI);
}
Math.sin(0);
Math.sin(Math.PI / 2);
Math.cos(0);
Math.cos(Math.PI);
Math.tan(Math.PI / 4);
Math.asin(1);
Math.acos(0);
Math.atan(1);
Math.atan2(1, 1);
Math.atan2(1, 0);
Other Useful Methods
| Method | Description | Example |
|---|
Math.fround(x) | Nearest 32-bit float | Math.fround(1.5) β 1.5 |
Math.clz32(x) | Leading zeros in 32-bit | Math.clz32(1) β 31 |
Math.imul(a, b) | 32-bit integer multiply | Math.imul(2, 4) β 8 |
Practical Examples
Circle Calculations
function circleArea(radius) {
return Math.PI * radius ** 2;
}
function circumference(radius) {
return 2 * Math.PI * radius;
}
circleArea(5);
circumference(5);
Distance and Angles
function distance(x1, y1, x2, y2) {
return Math.hypot(x2 - x1, y2 - y1);
}
function angle(x1, y1, x2, y2) {
const radians = Math.atan2(y2 - y1, x2 - x1);
return radians * (180 / Math.PI);
}
distance(0, 0, 3, 4);
angle(0, 0, 1, 1);
Clamping Values
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
clamp(150, 0, 100);
clamp(-50, 0, 100);
clamp(50, 0, 100);
Linear Interpolation
function lerp(start, end, t) {
return start + (end - start) * t;
}
lerp(0, 100, 0);
lerp(0, 100, 0.5);
lerp(0, 100, 1);
Mapping Range
function mapRange(value, inMin, inMax, outMin, outMax) {
return ((value - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;
}
mapRange(50, 0, 100, 0, 1);
mapRange(25, 0, 100, 0, 255);
Key Takeaways
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Math Object Summary β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β 1. Math is not a constructor - use methods directly β
β 2. Rounding: round, floor, ceil, trunc have different behavior β
β 3. Math.random() returns [0, 1) - includes 0, excludes 1 β
β 4. Trig functions use RADIANS, not degrees β
β 5. Use spread operator for min/max with arrays β
β 6. Math.hypot() is great for distance calculations β
β 7. ES2016 ** operator is cleaner than Math.pow() β
β 8. Math.sign() returns -1, 0, or 1 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ