JavaScript Math Functions

1. Math.floor(x)

Rounds a number down to the nearest integer.

console.log(Math.floor(4.9));  // 4
console.log(Math.floor(4.1));  // 4
console.log(Math.floor(-4.9)); // -5
console.log(Math.floor(-4.1)); // -5
console.log(Math.floor(0.99)); // 0
                

2. Math.ceil(x)

Rounds a number up to the nearest integer.

console.log(Math.ceil(4.1));  // 5
console.log(Math.ceil(4.9));  // 5
console.log(Math.ceil(-4.1)); // -4
console.log(Math.ceil(-4.9)); // -4
console.log(Math.ceil(0.01)); // 1
                

3. Math.round(x)

Rounds a number to the nearest integer.

console.log(Math.round(4.4));  // 4
console.log(Math.round(4.5));  // 5
console.log(Math.round(4.6));  // 5
console.log(Math.round(-4.4)); // -4
console.log(Math.round(-4.5)); // -4
console.log(Math.round(-4.6)); // -5
                

4. Math.trunc(x)

Removes the decimal part of a number.

console.log(Math.trunc(4.9));  // 4
console.log(Math.trunc(4.1));  // 4
console.log(Math.trunc(-4.9)); // -4
console.log(Math.trunc(-4.1)); // -4
console.log(Math.trunc(0.99)); // 0
                

5. Math.abs(x)

Returns the absolute value of a number.

console.log(Math.abs(4));   // 4
console.log(Math.abs(-4));  // 4
console.log(Math.abs(0));   // 0
console.log(Math.abs(-4.9));// 4.9
console.log(Math.abs(4.9)); // 4.9
                

6. Math.pow(base, exponent)

Calculates the power of a number (base^exponent).

console.log(Math.pow(2, 3));  // 8
console.log(Math.pow(5, 2));  // 25
console.log(Math.pow(7, 0));  // 1
console.log(Math.pow(-2, 3)); // -8
console.log(Math.pow(2, -2)); // 0.25
                

7. Math.sqrt(x)

Finds the square root of a number.

console.log(Math.sqrt(25));  // 5
console.log(Math.sqrt(9));   // 3
console.log(Math.sqrt(2));   // 1.414
console.log(Math.sqrt(0));   // 0
console.log(Math.sqrt(-4));  // NaN
                

8. Math.cbrt(x)

Finds the cube root of a number.

console.log(Math.cbrt(27));   // 3
console.log(Math.cbrt(8));    // 2
console.log(Math.cbrt(1));    // 1
console.log(Math.cbrt(-27));  // -3
console.log(Math.cbrt(125));  // 5
                

9. Math.max(x1, x2, ..., xn)

Returns the largest number from a given set of numbers.

console.log(Math.max(4, 7, 1, 9, 12)); // 12
console.log(Math.max(-1, -7, -5));     // -1
console.log(Math.max(3.5, 2.8, 3.9));  // 3.9
console.log(Math.max(100, 200, 50));   // 200
console.log(Math.max());               // -Infinity
                

10. Math.min(x1, x2, ..., xn)

Returns the smallest number from a given set of numbers.

console.log(Math.min(4, 7, 1, 9, 12)); // 1
console.log(Math.min(-1, -7, -5));     // -7
console.log(Math.min(3.5, 2.8, 3.9));  // 2.8
console.log(Math.min(100, 200, 50));   // 50
console.log(Math.min());               // Infinity
                

11. Math.random()

Generates a random decimal number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random());     
console.log(Math.random() * 10);
console.log(Math.floor(Math.random() * 100));
console.log(Math.ceil(Math.random() * 50));
console.log(Math.random() * 5 + 1);
                

12. .toFixed(n)

Rounds a number to n decimal places and returns it as a string.

console.log((5.6789).toFixed(2));  // "5.68"
console.log((3.14159).toFixed(3)); // "3.142"
console.log((100.567).toFixed(1)); // "100.6"
console.log((0.123456).toFixed(4));// "0.1235"
                

Summary Table

Function Description
Math.floor(x) Rounds down to the nearest integer
Math.ceil(x) Rounds up to the nearest integer
Math.round(x) Rounds to the nearest integer
Math.trunc(x) Removes the decimal part, returning only the integer part
Math.abs(x) Returns the absolute value of x
Math.pow(x, y) Returns x raised to the power of y
Math.sqrt(x) Returns the square root of x
Math.cbrt(x) Returns the cube root of x
Math.max(x, y, ...) Returns the largest value from the given numbers
Math.min(x, y, ...) Returns the smallest value from the given numbers
Math.random() Returns a random floating-point number between 0 and 1
x.toFixed(n) Formats a number with n decimal places

Compound Interest Calculation (Check console)


        function calculateCompoundInterest(P, r, t, n) {
            let a = P * Math.pow(1 + r / n, n * t);
            return (a - P).toFixed(2);
        }
                

Triangle Area Calculation (Check console)


        function calculateTriangleArea(a, b, c) {
            let s = (a + b + c) / 2;
            let area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
            return area.toFixed(2);
        }
                

Circle Properties Calculation (Check console)


        function calculateCircleProperties(r) {
            return [(2 * Math.PI * r).toFixed(2), (Math.PI * r * r).toFixed(2)];
        }