JavaScript Math Object


The Math object in JavaScript is a built-in object that provides a collection of mathematical constants and functions. It helps perform tasks like rounding, finding square roots, generating random numbers, and more.

You don’t need to create a Math object — just use Math.method().



1. Math Properties (Constants)

Property Description Example
Math.PI π (Pi) 3.14159...
Math.E Euler’s number 2.718...
Math.SQRT2 Square root of 2 1.414...
Math.LN2 Natural log of 2 0.693...

Example:

console.log("PI:", Math.PI);
console.log("Euler's number:", Math.E);


2. Rounding Numbers

Method Description Example
Math.round(x) Rounds to nearest integer Math.round(4.7) → 5
Math.floor(x) Rounds down to nearest integer Math.floor(4.9) → 4
Math.ceil(x) Rounds up to nearest integer Math.ceil(4.1) → 5
Math.trunc(x) Removes decimal part (no rounding) Math.trunc(4.9) → 4

Example:

console.log(Math.round(4.7));  // 5
console.log(Math.floor(4.7));  // 4
console.log(Math.ceil(4.1));   // 5
console.log(Math.trunc(4.9));  // 4


3. Math Functions

Function Description Example
Math.sqrt(x) Square root Math.sqrt(9) → 3
Math.pow(x, y) x to the power of y Math.pow(2, 3) → 8
Math.abs(x) Absolute value Math.abs(-5) → 5
Math.max(x, y, ...) Returns the largest number Math.max(1, 3, 7) → 7
Math.min(x, y, ...) Returns the smallest number Math.min(1, 3, 7) → 1

Example:

console.log(Math.sqrt(16));     // 4
console.log(Math.pow(2, 4));    // 16
console.log(Math.abs(-100));    // 100
console.log(Math.max(1, 5, 10));// 10
console.log(Math.min(1, 5, 10));// 1


4. Random Numbers

Function Description Example
Math.random() Returns a random number (0–1) Math.random() → 0.3745...


Example – Random Integer Between 1–10:

let randomNum = Math.floor(Math.random() * 10) + 1;
console.log(randomNum);


5. Trigonometric and Logarithmic Functions

Function Description Example
Math.sin(x) Sine (x in radians) Math.sin(Math.PI/2) → 1
Math.cos(x) Cosine Math.cos(0) → 1
Math.log(x) Natural log Math.log(1) → 0


Quick Recap: Common Math Methods

Task Use This Example
Round a number Math.round() Math.round(3.5)
Get square root Math.sqrt() Math.sqrt(25)
Find power of a number Math.pow() Math.pow(2, 3)
Get absolute value Math.abs() Math.abs(-9)
Generate random number Math.random() Math.random()
Round down or up Math.floor(), Math.ceil() Math.ceil(3.1)