The Math object in JavaScript is a built-in object that provides mathematical constants and functions.
It’s not a constructor, so you don’t use new Math().
You access methods like this:
Math.methodName(arguments) console.log(Math.E); // 2.718281828459045 (Euler's number)
console.log(Math.PI); // 3.141592653589793 (π)
console.log(Math.SQRT2); // 1.4142135623730951 (√2)
console.log(Math.SQRT1_2); // 0.7071067811865476 (√1/2)
console.log(Math.LN2); // 0.6931471805599453 (ln 2)
console.log(Math.LN10); // 2.302585092994046 (ln 10)
console.log(Math.LOG2E); // 1.4426950408889634 (log2(e))
console.log(Math.LOG10E); // 0.4342944819032518 (log10(e)) console.log(Math.abs(-7)); // 7 → absolute value
console.log(Math.sign(-10)); // -1 → negative
console.log(Math.sign(0)); // 0
console.log(Math.sign(8)); // 1 → positive
console.log(Math.trunc(5.89)); // 5 → removes decimals
console.log(Math.round(4.6)); // 5 → rounds to nearest integer
console.log(Math.ceil(4.1)); // 5 → rounds up
console.log(Math.floor(4.9)); // 4 → rounds down console.log(Math.pow(2, 3)); // 8 → 2³
console.log(Math.sqrt(16)); // 4 → √16
console.log(Math.cbrt(27)); // 3 → ∛27
console.log(Math.hypot(3, 4)); // 5 → √(3² + 4²) console.log(Math.exp(1)); // 2.718281828459045 → e¹
console.log(Math.expm1(1)); // 1.718281828459045 → e¹ - 1
console.log(Math.log(Math.E)); // 1 → natural log (base e)
console.log(Math.log10(100)); // 2 → base 10 log
console.log(Math.log2(8)); // 3 → base 2 log
console.log(Math.log1p(0.5)); // 0.4054651081081644 → log(1 + x) console.log(Math.sin(Math.PI / 2)); // 1 → sin(90°)
console.log(Math.cos(0)); // 1 → cos(0°)
console.log(Math.tan(Math.PI / 4)); // 1 → tan(45°)
console.log(Math.asin(1)); // 1.5707963267948966 → arcsin(1) = π/2
console.log(Math.acos(0)); // 1.5707963267948966 → arccos(0) = π/2
console.log(Math.atan(1)); // 0.7853981633974483 → arctan(1) = π/4
console.log(Math.atan2(1, 1)); // 0.7853981633974483 → atan2(y, x) console.log(Math.sinh(0)); // 0 → hyperbolic sine
console.log(Math.cosh(0)); // 1 → hyperbolic cosine
console.log(Math.tanh(0)); // 0 → hyperbolic tangent
console.log(Math.asinh(1)); // 0.881373587019543 → inverse hyperbolic sine
console.log(Math.acosh(1)); // 0 → inverse hyperbolic cosine
console.log(Math.atanh(0.5)); // 0.5493061443340549 → inverse hyperbolic tangent console.log(Math.random());
// Random number between 0 (inclusive) and 1 (exclusive)
console.log(Math.floor(Math.random() * 10));
// Random integer between 0 and 9
console.log(Math.floor(Math.random() * (20 - 10 + 1)) + 10);
// Random integer between 10 and 20 console.log(Math.max(1, 5, 10, 3)); // 10 → largest number
console.log(Math.min(1, 5, 10, 3)); // 1 → smallest number
// Clamping a number between 0 and 100:
let value = 120;
let clamped = Math.min(Math.max(value, 0), 100);
console.log(clamped); // 100 console.log(Math.imul(3, 4)); // 12 → integer multiplication (32-bit)
console.log(Math.fround(1.337)); // 1.3370000123977661 → 32-bit float
console.log(Math.clz32(1)); // 31 → count leading zeros in 32-bit binary
console.log(Math.atan2(10, 10)); // 0.7853981633974483 → angle in radians let radius = 5;
let area = Math.PI * Math.pow(radius, 2);
console.log(area); // 78.53981633974483 let length = Math.floor(Math.random() * (16 - 8 + 1)) + 8;
console.log(length); let x1 = 2, y1 = 3, x2 = 7, y2 = 8;
let distance = Math.hypot(x2 - x1, y2 - y1);
console.log(distance); // 7.0710678118654755 let degrees = 180;
let radians = degrees * (Math.PI / 180);
console.log(radians); // 3.141592653589793 | Category | Common Methods |
|---|---|
| Constants | E, PI, SQRT2, SQRT1_2, LN2, LN10, LOG2E, LOG10E |
| Rounding | abs, sign, trunc, round, ceil, floor |
| Power/Roots | pow, sqrt, cbrt, hypot |
| Exponential/Logs | exp, expm1, log, log10, log2, log1p |
| Trigonometry | sin, cos, tan, asin, acos, atan, atan2 |
| Hyperbolic | sinh, cosh, tanh, asinh, acosh, atanh |
| Random/Utility | random, min, max, fround, imul, clz32 |
Latest tech news and coding tips.
Every application that stores and manages data relies on a set of basic operations known…
PHP remains one of the most widely used server-side programming languages, powering platforms such as…
Danfo.js is an open-source JavaScript library designed for data manipulation, analysis, and machine learning. It provides…
JavaScript's async and await keywords revolutionized asynchronous programming by making asynchronous code look and behave more like synchronous code.…
Pretty Good Privacy (PGP) is one of the most widely used encryption systems for securing emails,…
Database migration is one of the most challenging tasks in software engineering. While both PostgreSQL…