JavaScript Math Reference

Complete guide to JavaScript Math object methods and properties

🧮 What is the Math Object?

The JavaScript Math object provides mathematical constants and functions. It's a built-in object that doesn't need to be created - just use Math.method() directly.


// Simple Math example
console.log(Math.PI); // 3.141592653589793
console.log(Math.round(4.7)); // 5
                                    

Output:

3.141592653589793
5

Math Constants

🥧

Math.PI

The value of π (pi)

console.log(Math.PI);
// 3.141592653589793
📐

Math.E

Euler's number (e)

console.log(Math.E);
// 2.718281828459045

Math.SQRT2

Square root of 2

console.log(Math.SQRT2);
// 1.4142135623730951
📊

Math.LN2

Natural logarithm of 2

console.log(Math.LN2);
// 0.6931471805599453

🔹 Rounding Methods

Different ways to round numbers:

let num = 4.7;
let negNum = -4.7;

// Round to nearest integer
console.log("Math.round(4.7):", Math.round(num)); // 5
console.log("Math.round(-4.7):", Math.round(negNum)); // -5

// Round up (ceiling)
console.log("Math.ceil(4.1):", Math.ceil(4.1)); // 5
console.log("Math.ceil(-4.7):", Math.ceil(-4.7)); // -4

// Round down (floor)
console.log("Math.floor(4.7):", Math.floor(4.7)); // 4
console.log("Math.floor(-4.1):", Math.floor(-4.1)); // -5

// Truncate (remove decimal part)
console.log("Math.trunc(4.7):", Math.trunc(4.7)); // 4
console.log("Math.trunc(-4.7):", Math.trunc(-4.7)); // -4

Output:

Math.round(4.7): 5
Math.round(-4.7): -5
Math.ceil(4.1): 5
Math.ceil(-4.7): -4
Math.floor(4.7): 4
Math.floor(-4.1): -5
Math.trunc(4.7): 4
Math.trunc(-4.7): -4

🔹 Power and Root Methods

Mathematical power and root calculations:

// Power calculations
console.log("Math.pow(2, 3):", Math.pow(2, 3)); // 8 (2³)
console.log("Math.pow(4, 0.5):", Math.pow(4, 0.5)); // 2 (√4)

// Square root
console.log("Math.sqrt(16):", Math.sqrt(16)); // 4
console.log("Math.sqrt(2):", Math.sqrt(2)); // 1.4142135623730951

// Cube root
console.log("Math.cbrt(8):", Math.cbrt(8)); // 2
console.log("Math.cbrt(27):", Math.cbrt(27)); // 3

// Exponential (e^x)
console.log("Math.exp(1):", Math.exp(1)); // 2.718281828459045 (e¹)
console.log("Math.exp(2):", Math.exp(2)); // 7.38905609893065 (e²)

// Logarithms
console.log("Math.log(Math.E):", Math.log(Math.E)); // 1 (natural log)
console.log("Math.log10(100):", Math.log10(100)); // 2 (base 10 log)
console.log("Math.log2(8):", Math.log2(8)); // 3 (base 2 log)

Output:

Math.pow(2, 3): 8
Math.pow(4, 0.5): 2
Math.sqrt(16): 4
Math.sqrt(2): 1.4142135623730951
Math.cbrt(8): 2
Math.cbrt(27): 3
Math.exp(1): 2.718281828459045
Math.exp(2): 7.38905609893065
Math.log(Math.E): 1
Math.log10(100): 2
Math.log2(8): 3

🔹 Trigonometric Methods

Sine, cosine, and tangent functions (angles in radians):

// Basic trigonometric functions
console.log("Math.sin(Math.PI/2):", Math.sin(Math.PI/2)); // 1 (sin 90°)
console.log("Math.cos(0):", Math.cos(0)); // 1 (cos 0°)
console.log("Math.tan(Math.PI/4):", Math.tan(Math.PI/4)); // 1 (tan 45°)

// Inverse trigonometric functions
console.log("Math.asin(1):", Math.asin(1)); // 1.5707963267948966 (π/2)
console.log("Math.acos(1):", Math.acos(1)); // 0
console.log("Math.atan(1):", Math.atan(1)); // 0.7853981633974483 (π/4)

// Convert degrees to radians and back
function degreesToRadians(degrees) {
    return degrees * (Math.PI / 180);
}

function radiansToDegrees(radians) {
    return radians * (180 / Math.PI);
}

// Examples with degrees
let angle45 = degreesToRadians(45);
console.log("sin(45°):", Math.sin(angle45)); // 0.7071067811865476
console.log("cos(45°):", Math.cos(angle45)); // 0.7071067811865476

Output:

Math.sin(Math.PI/2): 1
Math.cos(0): 1
Math.tan(Math.PI/4): 1
Math.asin(1): 1.5707963267948966
Math.acos(1): 0
Math.atan(1): 0.7853981633974483
sin(45°): 0.7071067811865476
cos(45°): 0.7071067811865476

🔹 Min, Max, and Absolute

Finding minimum, maximum, and absolute values:

// Find minimum and maximum
console.log("Math.min(5, 3, 9, 1):", Math.min(5, 3, 9, 1)); // 1
console.log("Math.max(5, 3, 9, 1):", Math.max(5, 3, 9, 1)); // 9

// With arrays (use spread operator)
let numbers = [5, 3, 9, 1, 7];
console.log("Min from array:", Math.min(...numbers)); // 1
console.log("Max from array:", Math.max(...numbers)); // 9

// Absolute value (distance from zero)
console.log("Math.abs(-5):", Math.abs(-5)); // 5
console.log("Math.abs(5):", Math.abs(5)); // 5
console.log("Math.abs(-3.14):", Math.abs(-3.14)); // 3.14

// Sign function
console.log("Math.sign(-5):", Math.sign(-5)); // -1
console.log("Math.sign(5):", Math.sign(5)); // 1
console.log("Math.sign(0):", Math.sign(0)); // 0

Output:

Math.min(5, 3, 9, 1): 1
Math.max(5, 3, 9, 1): 9
Min from array: 1
Max from array: 9
Math.abs(-5): 5
Math.abs(5): 5
Math.abs(-3.14): 3.14
Math.sign(-5): -1
Math.sign(5): 1
Math.sign(0): 0

🔹 Practical Examples

Real-world applications of Math methods:

// Calculate circle area and circumference
function circleCalculations(radius) {
    let area = Math.PI * Math.pow(radius, 2);
    let circumference = 2 * Math.PI * radius;
    
    return {
        area: Math.round(area * 100) / 100, // Round to 2 decimals
        circumference: Math.round(circumference * 100) / 100
    };
}

let circle = circleCalculations(5);
console.log("Circle with radius 5:");
console.log("Area:", circle.area); // 78.54
console.log("Circumference:", circle.circumference); // 31.42

// Distance between two points
function distance(x1, y1, x2, y2) {
    let dx = x2 - x1;
    let dy = y2 - y1;
    return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
}

console.log("Distance between (0,0) and (3,4):", distance(0, 0, 3, 4)); // 5

// Generate random integer between min and max
function randomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

console.log("Random number 1-10:", randomInt(1, 10));

Output:

Circle with radius 5:
Area: 78.54
Circumference: 31.42
Distance between (0,0) and (3,4): 5
Random number 1-10: [varies]

🧠 Test Your Knowledge

What does Math.ceil(4.1) return?