Javascript Math Reference

Javascript Math Introduction

Javascript supports a large number of math fucntions which are essentially methods of javascript built in math object. Let us take a look at the following code snippent which calculates the x power y using pow(x,y) math function.
<script type="text/javascript">

Example - Usage of Math.pow
var x =4, y =3;
var z = Math.pow(x,y);

document.write ("Math.pow(4,3) is : " + z);

Notice that all math fuctions are called like Math.pow(4,3) and not just pow(4,3). You may like to try this program online here . Make changes in the program and see its results.

Here is the list of all the math functions supported by javascript.

We have split the table in two categories. The first category contains the functions which are more widely used and you may like to spend some time to make yourself familar with it. If you heavily uses math functions in javascript you may like to memorize these

The second table contains the less frequently used math fuctions ( like acosine etc). You may refer them as and when you need them.

Table 1: Javascript Math Functions - Important ones

Function Meaning Example Usage Result
Calculate the absolute value of x Math.abs(-4); 4
Calculate x to the power y Math.pow(4,3) 64
Calculate Square root of x Math.sqrt(25); 5
Returns the max value of (x,y) Math.max(13,9) 13
Returns the min value of (x,y) Math.min(2,5); 2
Returns a random number between 0 and 1 Math.random() Any random Number
Rounds x to the nearest integer Math.round(2.49);
Returns x, rounded downwards to the nearest integer Math.floor(8.97) 8
Returns x, rounded upwards to the nearest integer Math.ceil(8.97); 9

Table 2: Javascript Math Functions - Less Commonly used

Function Meaning Example Usage Result
Calculate the acosine of x Math.acosx(0); ??
Calculate x to the power y pow(4,3) 64
Calculate the absolute value of x Math.abs(-4); 4
Calculate x to the power y pow(4,3) 64