JavaScript Math
Object
The Math object is used to perform common mathematical tasks.
Online Examples
Complete Math Object Reference
We provide a JavaScript Math Object Reference that includes all properties and methods available for the Math object.
This reference contains detailed descriptions of each property and method, along with relevant examples.
Math Object
The Math object is used to perform ordinary mathematical tasks.
The Math object provides various mathematical constants and functions. You do not need to define it before use.
Syntax for using Math properties/methods:
Example
var x = Math.PI;
var y = Math.sqrt(16);
Note: The Math object does not need to be defined before use.
Mathematical Constants
JavaScript provides 8 mathematical constants that can be accessed via the Math object:
You can refer to the following JavaScript constants:
Example
Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
Mathematical Methods
In addition to the mathematical constants accessible via the Math object, there are several functions (methods) available.
The following example uses the round method of the Math object to round a number to the nearest integer:
document.write(Math.round(4.7));
The above code outputs:
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 outputs:
The following example uses the floor() method and random() method of the Math object to return a random number between 0 and 11:
document.write(Math.floor(Math.random() * 11));
The above code outputs: