JavaScript Math Object


The purpose of the Math object is to perform common arithmetic tasks.


Examples

Online Examples

round()
How to use round().

random()
Use random() to return a random number between 0 and 1.

max()
Uses max() to return the larger of two given numbers. (Prior to ECMASCript v3, the method had only two parameters.) )

min()
Uses min() to return the smaller of two given numbers. (Prior to ECMASCript v3, the method had only two parameters.) )


Complete Math object references

We provide reference manual for JavaScript Math objects, which includes all the properties and methods available for arithmetic objects.

the manual contains a detailed description of each property and method, as well as related examples.


Math object

The purpose of the Math object is to perform ordinary arithmetic tasks.

The Math object provides a variety of numeric types and functions. You do not need to define this object before using it.

Syntax for using Math's property/method:

Example

var x=Math.PI;
var y=Math.sqrt(16);

Note: The Math object does not need to be defined before using the object.


Calculate the value

JavaScript provides 8 arithmetic values that can be accessed by Math objects:

You can refer to the following Javascript constant usage methods:

Example

Math.E

Math.PI

Math.SQRT2

Math.SQRT1_2

Math.LN2

Math.LN10

Math.LOG2E

Math.LOG10E


Arithmetic method

In addition to the arithmetic values that can be accessed by the Math object, there are several functions that can be used.

The following example uses the round method of the Math object to round a number.

document.write(Math.round(4.7));

the above code output is:

5

The following example uses the random() method of the Math object to return a random number between 0 and 1:

document.write(Math.random());

the above code output is:

0.7461656395525575

The following example uses the Math object's floor() method and random() to return a random number between 0 and 11:

document.write(Math.floor(Math.random()*11));

the above code output is:

5