JavaScript Math

Mathematical operations and functions in JavaScript

🔢 JavaScript Math Object

The Math object provides mathematical constants and functions. It's built into JavaScript and doesn't need to be created.


// Basic math operations
console.log(Math.PI);           // 3.141592653589793
console.log(Math.round(4.7));   // 5
console.log(Math.max(1, 3, 2)); // 3
                                    

Output:

3.141592653589793
5
3

Common Math Methods

🔄

Rounding

Round numbers up, down, or nearest

Math.round(4.7); // 5
Math.ceil(4.1);  // 5
Math.floor(4.9); // 4
📊

Min/Max

Find smallest or largest values

Math.min(1, 3, 2); // 1
Math.max(1, 3, 2); // 3

Power/Root

Calculate powers and square roots

Math.pow(2, 3);  // 8
Math.sqrt(16);   // 4
🎲

Random

Generate random numbers

Math.random(); // 0.123...

🔹 Math Constants

JavaScript Math object provides useful mathematical constants:

// Mathematical constants
console.log(Math.PI);       // 3.141592653589793 (π)
console.log(Math.E);        // 2.718281828459045 (Euler's number)
console.log(Math.LN2);      // 0.6931471805599453 (ln(2))
console.log(Math.LN10);     // 2.302585092994046 (ln(10))
console.log(Math.LOG2E);    // 1.4426950408889634 (log₂(e))
console.log(Math.LOG10E);   // 0.4342944819032518 (log₁₀(e))
console.log(Math.SQRT1_2);  // 0.7071067811865476 (√½)
console.log(Math.SQRT2);    // 1.4142135623730951 (√2)

Output:

3.141592653589793
2.718281828459045
0.6931471805599453
2.302585092994046
1.4426950408889634
0.4342944819032518
0.7071067811865476
1.4142135623730951

🔹 Rounding Methods

Different ways to round numbers:

let number = 4.7;

// Round to nearest integer
console.log(Math.round(number));  // 5

// Round up (ceiling)
console.log(Math.ceil(number));   // 5

// Round down (floor)
console.log(Math.floor(number));  // 4

// Remove decimal part (truncate)
console.log(Math.trunc(number));  // 4

// Examples with negative numbers
console.log(Math.round(-4.7));    // -5
console.log(Math.ceil(-4.7));     // -4
console.log(Math.floor(-4.7));    // -5

Output:

5
5
4
4
-5
-4
-5

🔹 Random Numbers

Generate random numbers for different purposes:

// Random decimal between 0 and 1
console.log(Math.random()); // 0.123456789...

// Random integer between 1 and 10
let randomInt = Math.floor(Math.random() * 10) + 1;
console.log(randomInt); // 1, 2, 3, ... 10

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

console.log(randomBetween(5, 15)); // Random number between 5 and 15

// Random array element
let colors = ["red", "blue", "green", "yellow"];
let randomColor = colors[Math.floor(Math.random() * colors.length)];
console.log(randomColor); // Random color from array

Output:

0.7834521098765432
7
12
green

🔹 Power and Root Operations

Calculate powers, roots, and exponentials:

// Power operations
console.log(Math.pow(2, 3));    // 8 (2³)
console.log(Math.pow(5, 2));    // 25 (5²)
console.log(2 ** 3);            // 8 (ES6 exponentiation operator)

// Square root
console.log(Math.sqrt(16));     // 4
console.log(Math.sqrt(25));     // 5

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

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

// Logarithms
console.log(Math.log(Math.E));  // 1 (ln(e))
console.log(Math.log10(100));   // 2 (log₁₀(100))
console.log(Math.log2(8));      // 3 (log₂(8))

Output:

8
25
8
4
5
2
3
2.718281828459045
7.38905609893065
1
2
3

🔹 Practical Math Examples

Real-world applications of JavaScript Math:

// Calculate circle area
function circleArea(radius) {
    return Math.PI * Math.pow(radius, 2);
}
console.log(circleArea(5)); // 78.53981633974483

// Calculate distance between two points
function distance(x1, y1, x2, y2) {
    return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
console.log(distance(0, 0, 3, 4)); // 5

// Convert degrees to radians
function toRadians(degrees) {
    return degrees * (Math.PI / 180);
}
console.log(toRadians(90)); // 1.5707963267948966

// Generate random password
function generateRandomNumber(length) {
    let result = '';
    for (let i = 0; i < length; i++) {
        result += Math.floor(Math.random() * 10);
    }
    return result;
}
console.log(generateRandomNumber(6)); // Random 6-digit number

Output:

78.53981633974483
5
1.5707963267948966
847293

🧠 Test Your Knowledge

What does Math.ceil(4.1) return?