JavaScript Math Object

previous next


The Math object allows you to perform common mathematical tasks.


Math Object

The Math object allows you to perform common mathematical tasks.

The Math object includes several mathematical values and functions. You do not need to define the Math object before using it.


Mathematical Values

JavaScript provides eight mathematical values (constants) that can be accessed from the Math object. These are: E, PI, square root of 2, square root of 1/2, natural log of 2, natural log of 10, base-2 log of E, and base-10 log of E.

You may reference these values from your JavaScript like this:

Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E

 


Mathematical Methods

In addition to the mathematical values that can be accessed from the Math object there are also several functions (methods) available.

Examples of functions (methods):

The following example uses the round() method of the Math object to round a number to the nearest integer:

document.write(Math.round(21.8))

The code above will result in the following output:

22

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 code above can result in the following output:

The following example uses the floor() and random() methods of the Math object to return a random number between 0 and 100:

document.write(Math.floor(Math.random()*101))

The code above can result in the following output:

 


Math Object Methods

Method

Description

abs(x) Returns the absolute value of a number
acos(x) Returns the arccosine of a number
asin(x) Returns the arcsine of a number
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y,x) Returns the angle theta of an (x,y) point as a numeric value between -PI and PI radians
ceil(x) Returns the value of a number rounded upwards to the nearest integer
cos(x) Returns the cosine of a number
exp(x) Returns the value of Ex
floor(x) Returns the value of a number rounded downwards to the nearest integer
log(x) Returns the natural logarithm (base E) of a number
max(x,y) Returns the number with the highest value of x and y
min(x,y) Returns the number with the lowest value of x and y
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 a number to the nearest integer
sin(x) Returns the sine of a number
sqrt(x) Returns the square root of a number
tan(x) Returns the tangent of an angle
toSource() Represents the source code of an object
valueOf() Returns the primitive value of a Math object

 


Math Object Properties

Property

Description

constructor A reference to the function that created the object
E Returns Euler's constant (approx. 2.718)
LN2 Returns the natural logarithm of 2 (approx. 0.693)
LN10 Returns the natural logarithm of 10 (approx. 2.302)
LOG2E Returns the base-2 logarithm of E (approx. 1.442)
LOG10E Returns the base-10 logarithm of E (approx. 0.434)
PI Returns PI (approx. 3.14159)
prototype Allows you to add properties and methods to the object
SQRT1_2 Returns the square root of 1/2 (approx. 0.707)
SQRT2 Returns the square root of 2 (approx. 1.414)

 


Try it

To see how HTML and JavaScript work, you can only push the submit button, or you can make your own HTML and JavaScript code.

           

 


previous next