Javascript Math

Math object is important thing in development. If you want to make some calculations, you will need Math functions for doing it easily and quickly.

Javascript Math Object

Javascript has global Math object for mathematics, its functions and constants.

Let's see some Math functions and constants, which are more useful .

Math.Random()

Its very popular and useful. Here is how to use it.

Syntax.

Math.random()  // returns random number between [0,1]

<!DOCTYPE html>
<html>
  <title>W3Docs.com</title>
  <body>

    <button onclick="myRandom()">Click Me</button>
    <p>random: <span id="element"></span></p>

    <script>
      function myRandom() {
        document.getElementById("element").innerHTML = Math.random();
      }
    </script>

  </body>
</html>

Math.min() & Math.max()

Here are two useful functions from Math. Min or Max functions are useful everywhere.

Syntax.

Math.min(3.2, -158, -10.2, 8.3)  // returns -158
Math.max(3.2, -158, -10.2, 8.3)  // returns 8.3

<!DOCTYPE html>
<html>
  <title>W3Docs.com</title>
  <body>

    <button onclick="myFunc()">Click Me</button>
    <p>Min: <span id="element1"></span></p>
    <p>Max: <span id="element2"></span></p>

    <script>
      function myFunc() {
        document.getElementById("element1").innerHTML = Math.min(3.2, -158, -10.2, 8.3);
        document.getElementById("element2").innerHTML = Math.max(3.2, -158, -10.2, 8.3);
      }
    </script>

  </body>
</html>

Math.round()

Now Let's talk a little about math round function. Math.round() function returns round of number to near (up or down).

Syntax.

Math.round(1.7);     // returns 2
Math.round(1.4);     // returns 1

<!DOCTYPE html>
<html>
  <title>W3Docs.com</title>
  <body>

    <button onclick="myRound()">Click Me</button>
    <p>round 1.4: <span id="element"></span></p>

    <script>
      function myRound() {
        document.getElementById("element").innerHTML = Math.round(1.4);
      }
    </script>

  </body>
</html>

Math.ceil()

Math.ceil() function is very useful function. As an example you can use it, when you want to create a pagination, when you have items count and pages count.

Syntax.

Math.ceil(1.2);     // returns 2

<!DOCTYPE html>
<html>
  <title>W3Docs.com</title>
  <body>

    <button onclick="myCeil()">Click Me</button>
    <p>ceil 1.4: <span id="element"></span></p>

    <script>
      function myCeil() {
        document.getElementById("element").innerHTML = Math.ceil(1.4);
      }
    </script>

  </body>
</html>

Math.floor()

Math.floor() function rounds the number down to nearist

Syntax.

Math.floor(1.7);     // returns 1

<!DOCTYPE html>
<html>
  <title>W3Docs.com</title>
  <body>

    <button onclick="myFloor()">Click Me</button>
    <p>floor 1.7: <span id="element"></span></p>

    <script>
      function myFloor() {
        document.getElementById("element").innerHTML = Math.floor(1.7);
      }
    </script>

  </body>
</html>

Math constants

As you know, Math has some constants. You can use them in your application like this:

Syntax.

Math.E;         // returns Euler's number
Math.PI         // returns PI
Math.SQRT2      // returns the square root of 2
Math.SQRT1_2    // returns the square root of 1/2
Math.LN2        // returns the natural logarithm of 2
Math.LN10       // returns the natural logarithm of 10
Math.LOG2E      // returns base 2 logarithm of E
Math.LOG10E     // returns base 10 logarithm of E

Here is the list of Math functions

Method Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
asin(x) Returns the arcsine of x, in radians
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y,x) Returns the arctangent of the quotient of its arguments
ceil(x) Returns x, rounded upwards to the nearest integer
cos(x) Returns the cosine of x (x is in radians)
exp(x) Returns the value of Ex
floor(x) Returns x, rounded downwards to the nearest integer
log(x) Returns the natural logarithm (base E) of x
max(x,y,z,...,n) Returns the number with the highest value
min(x,y,z,...,n) Returns the number with the lowest value
pow(x,y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Rounds x to the nearest integer
sin(x) Returns the sine of x (x is in radians)
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle