javascript
  1. javascript-math

JavaScript Math

The JavaScript Math object provides a wide variety of mathematical functions. These are useful for performing calculations, generating random numbers, and formatting numbers for display purposes.

Syntax

All Math functions are called by the syntax Math.function_name(). Some of the most commonly used functions are:

  • Math.random(): returns a random number between 0 (inclusive) and 1 (exclusive).
  • Math.floor(): returns the largest integer less than or equal to a given number.
  • Math.ceil(): returns the smallest integer greater than or equal to a given number.
  • Math.round(): rounds a given number to the nearest integer.
  • Math.pow(): returns the base to the exponent power.
  • Math.sqrt(): returns the square root of a given number.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Math Function Example</title>
</head>
<body>

<!-- Placeholder for displaying the output -->
<p id="output"></p>

<script>
    // Math functions example
    var num1 = 9;
    var num2 = 4;

    // Basic arithmetic operations
    var sum = num1 + num2;
    var difference = num1 - num2;
    var product = num1 * num2;
    var quotient = num1 / num2;

    // Display basic arithmetic results
    document.getElementById("output").innerHTML += "Sum: " + sum + "<br>";
    document.getElementById("output").innerHTML += "Difference: " + difference + "<br>";
    document.getElementById("output").innerHTML += "Product: " + product + "<br>";
    document.getElementById("output").innerHTML += "Quotient: " + quotient + "<br>";

    // Other Math functions
    var squareRoot = Math.sqrt(num1);
    var absoluteValue = Math.abs(difference);
    var power = Math.pow(num1, 2);
    var randomNum = Math.random();

    // Display results of other Math functions
    document.getElementById("output").innerHTML += "Square Root: " + squareRoot + "<br>";
    document.getElementById("output").innerHTML += "Absolute Value: " + absoluteValue + "<br>";
    document.getElementById("output").innerHTML += "Power of 2: " + power + "<br>";
    document.getElementById("output").innerHTML += "Random Number between 0 and 1: " + randomNum;
</script>

</body>
</html>

Output

The above code will output:

Sum: 13
Difference: 5
Product: 36
Quotient: 2.25
Square Root: 3
Absolute Value: 5
Power of 2: 81
Random Number between 0 and 1: 0.5448967296056955

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Math Function Example</title>
</head>
<body>

<!-- Placeholder for displaying the output -->
<p id="output"></p>

<script>
    // Math functions example
    var num1 = 9;
    var num2 = 4;

    // Basic arithmetic operations
    var sum = num1 + num2;
    var difference = num1 - num2;
    var product = num1 * num2;
    var quotient = num1 / num2;

    // Display basic arithmetic results
    document.getElementById("output").innerHTML += "Sum: " + sum + "<br>";
    document.getElementById("output").innerHTML += "Difference: " + difference + "<br>";
    document.getElementById("output").innerHTML += "Product: " + product + "<br>";
    document.getElementById("output").innerHTML += "Quotient: " + quotient + "<br>";

    // Other Math functions
    var squareRoot = Math.sqrt(num1);
    var absoluteValue = Math.abs(difference);
    var power = Math.pow(num1, 2);
    var randomNum = Math.random();

    // Display results of other Math functions
    document.getElementById("output").innerHTML += "Square Root: " + squareRoot + "<br>";
    document.getElementById("output").innerHTML += "Absolute Value: " + absoluteValue + "<br>";
    document.getElementById("output").innerHTML += "Power of 2: " + power + "<br>";
    document.getElementById("output").innerHTML += "Random Number between 0 and 1: " + randomNum;
</script>

</body>
</html>
Try Playground

Explanation

  • The first example uses Math.random() to generate a random number between 1 and 10. To ensure the number is between 1 and 10, we multiply the result of Math.random() by 10, then use Math.floor() to round down to the nearest integer. Finally, we add 1 to shift the range from 0-9 to 1-10.
  • The second example uses Math.round() to round a given number to the nearest 10. We divide the number by 10, round it to the nearest integer with Math.round(), then multiply it by 10 to get the rounded value.
  • The third example demonstrates the usage of Math.pow() and Math.PI to calculate the area of a circle with a given radius.

Use

JavaScript Math functions are useful for a variety of tasks, such as:

  • Generating random numbers for games or simulations
  • Formatting numbers for display purposes
  • Performing mathematical calculations in web applications

Important Points

Here are some important points to keep in mind when using Math functions:

  • Math is a built-in object in JavaScript, so it doesn't need to be imported or included in your code.
  • Many Math functions return a value that can be used in subsequent calculations.
  • Some Math functions, such as Math.random(), are designed to be used in conjunction with other functions to generate more complex behavior.

Summary

The JavaScript Math object provides a variety of mathematical functions that can be used for performing calculations, generating random numbers, and formatting numbers for display purposes. Its syntax is simple and most functions return a value that can be used in subsequent calculations.

Published on: