JavaScript Math Object
THE WORLD'S LARGEST WEB DEVELOPER SITE
×

JS Tutorial

JS HOME JS Introduction JS Where To JS Output JS Statements JS Syntax JS Comments JS Variables JS Operators JS Arithmetic JS Assignment JS Data Types JS Functions JS Objects JS Scope JS Events JS Strings JS String Methods JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Dates JS Date Formats JS Date Get Methods JS Date Set Methods JS Math JS Random JS Booleans JS Comparisons JS Conditions JS Switch JS Loop For JS Loop While JS Break JS Type Conversion JS Bitwise JS RegExp JS Errors JS Debugging JS Hoisting JS Strict Mode JS Style Guide JS Best Practices JS Mistakes JS Performance JS Reserved Words JS Versions JS JSON

JS Forms

JS Forms Forms API

JS Objects

Object Definitions Object Properties Object Methods Object Constructors Object Prototypes

JS Functions

Function Definitions Function Parameters Function Invocation Function Call Function Apply Function Closures

JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM CSS DOM Animations DOM Events DOM Event Listener DOM Navigation DOM Nodes DOM Collections DOM Node Lists

JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS AJAX

AJAX Intro AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

JS JSON

JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Objects JSON Arrays JSON Parse JSON Stringify JSON PHP JSON HTML JSON JSONP

JS Examples

JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Quiz JS Certificate

JS References

JavaScript Objects HTML DOM Objects


JavaScript Math Object


The JavaScript Math object allows you to perform mathematical tasks on numbers.


Example

Math.PI;            // returns 3.141592653589793
Try it Yourself »

Math.round()

Math.round(x) returns the value of x rounded to its nearest integer:

Example

Math.round(4.7);    // returns 5
Math.round(4.4);    // returns 4
Try it Yourself »

Math.pow()

Math.pow(x, y) returns the value of x to the power of y:

Example

Math.pow(8, 2);      // returns 64
Try it Yourself »

Math.sqrt()

Math.sqrt(x) returns the square root of x:

Example

Math.sqrt(64);      // returns 8
Try it Yourself »


Math.abs()

Math.abs(x) returns the absolute (positive) value of x:

Example

Math.abs(-4.7);     // returns 4.7
Try it Yourself »

Math.ceil()

Math.ceil(x) returns the value of x rounded up to its nearest integer:

Example

Math.ceil(4.4);     // returns 5
Try it Yourself »

Math.floor()

Math.floor(x) returns the value of x rounded down to its nearest integer:

Example

Math.floor(4.7);    // returns 4
Try it Yourself »

Math.sin()

Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).

If you want to use degrees instead of radians, you have to convert degrees to radians:

Angle in radians = Angle in degrees x PI / 180.

Example

Math.sin(90 * Math.PI / 180);     // returns 1 (the sine of 90 degrees)
Try it Yourself »

Math.cos()

Math.cos(x) returns the cosine (a value between -1 and 1) of the angle x (given in radians).

If you want to use degrees instead of radians, you have to convert degrees to radians:

Angle in radians = Angle in degrees x PI / 180.

Example

Math.cos(0 * Math.PI / 180);     // returns 1 (the cos of 0 degrees)
Try it Yourself »

Math.min() and Math.max()

Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:

Example

Math.min(0, 150, 30, 20, -8, -200);  // returns -200
Try it Yourself »

Example

Math.max(0, 150, 30, 20, -8, -200);  // returns 150
Try it Yourself »

Math.random()

Math.random() returns a random number between 0 (inclusive),  and 1 (exclusive):

Example

Math.random();     // returns a random number
Try it Yourself »

You will learn more about Math.random() in the next chapter of this tutorial.


Math Properties (Constants)

JavaScript provides 8 mathematical constants that can be accessed with the Math object:

Example

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
Try it Yourself »

Math Constructor

Unlike other global objects, the Math object has no constructor. Methods and properties are static.

All methods and properties (constants) can be used without creating a Math object first.


Math Object Methods

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 the value of x rounded up to its nearest integer
cos(x) Returns the cosine of x (x is in radians)
exp(x) Returns the value of Ex
floor(x) Returns the value of x rounded down to its 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) Returns the value of x rounded to its 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

Complete Math Reference

For a complete reference, go to our complete Math object reference.

The reference contains descriptions and examples of all Math properties and methods.


Test Yourself with Exercises!

Exercise 1 »   Exercise 2 »   Exercise 3 »   Exercise 4 »