JavaScript Math Object

By | August 24, 2022

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.PI</h2>
<p>Math.PI returns the ratio of a circle's circumference to its diameter:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.PI;
</script>
</body>
</html>

Result:

JavaScript Math.PI

Math.PI returns the ratio of a circle’s circumference to its diameter:

3.141592653589793

The Math Object

Unlike other objects, the Math object has no constructor.

The Math object is static.

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

Math Properties (Constants)

The syntax for any Math property is : Math.property.

JavaScript provides 8 mathematical constants that can be accessed as Math properties:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math Constants</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 
"<p><b>Math.E:</b> " + Math.E + "</p>" +
"<p><b>Math.PI:</b> " + Math.PI + "</p>" +
"<p><b>Math.SQRT2:</b> " + Math.SQRT2 + "</p>" +
"<p><b>Math.SQRT1_2:</b> " + Math.SQRT1_2 + "</p>" +
"<p><b>Math.LN2:</b> " + Math.LN2 + "</p>" +
"<p><b>Math.LN10:</b> " + Math.LN10 + "</p>" +
"<p><b>Math.LOG2E:</b> " + Math.LOG2E + "</p>" +
"<p><b>Math.Log10E:</b> " + Math.LOG10E + "</p>";
</script>
</body>
</html>

Result:

JavaScript Math Constants

Math.E: 2.718281828459045

Math.PI: 3.141592653589793

Math.SQRT2: 1.4142135623730951

Math.SQRT1_2: 0.7071067811865476

Math.LN2: 0.6931471805599453

Math.LN10: 2.302585092994046

Math.LOG2E: 1.4426950408889634

Math.Log10E: 0.4342944819032518

Math Methods

The syntax for Math any methods is : Math.method(number)

Number to Integer

There are 4 common methods to round a number to an integer:

Math.round(x)Returns x rounded to its nearest integer
Math.ceil(x)Returns x rounded up to its nearest integer
Math.floor(x)Returns x rounded down to its nearest integer
Math.trunc(x)Returns the integer part of x (new in ES6)

Math.round()

Math.round(x) returns the nearest integer:

Examples

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.6);
</script>
</body>
</html>

Result:

JavaScript Math.round()

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

5

Examples

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.5);
</script>
</body>
</html>

Result:

JavaScript Math.round()

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

5

Examples

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.4);
</script>
</body>
</html>

Result:

JavaScript Math.round()

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

4

Math.ceil()

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.ceil()</h2>
<p>Math.ceil() rounds a number <strong>up</strong> to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.ceil(4.4);
</script>
</body>
</html>

Result:

JavaScript Math.ceil()

Math.ceil() rounds a number up to its nearest integer:

5

Math.floor()

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.floor()</h2>
<p>Math.floor(x) returns the value of x rounded <strong>down</strong> to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(4.7);
</script>
</body>
</html>

Result:

JavaScript Math.floor()

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

4

Math.trunc()

Math.trunc(x) returns the integer part of x:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.trunc()</h2>
<p>Math.trunc(x) returns the integer part of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.trunc(4.7);
</script>
</body>
</html>

Result:

JavaScript Math.trunc()

Math.trunc(x) returns the integer part of x:

4

Math.sign()

Math.sign(x) returns if x is negative, null or positive:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sign()</h2>
<p>Math.sign(x) returns if x is negative, null or positive:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sign(4);
</script>
</body>
</html>

Result:

JavaScript Math.sign()

Math.sign(x) returns if x is negative, null or positive:

1

Math.pow()

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.pow()</h2>
<p>Math.pow(x,y) returns the value of x to the power of y:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.pow(8,2);
</script>
</body>
</html>

Result:

JavaScript Math.pow()

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

64

Math.sqrt()

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sqrt()</h2>
<p>Math.sqrt(x) returns the square root of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sqrt(64);
</script>
</body>
</html>

Result:

JavaScript Math.sqrt()

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

8

Math.abs()

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.abs()</h2>
<p>Math.abs(x) returns the absolute (positive) value of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.abs(-4.7);
</script>
</body>
</html>

Result:

JavaScript Math.abs()

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

4.7

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

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sin()</h2>
<p>Math.sin(x) returns the sin of x (given in radians):</p>
<p>Angle in radians = (angle in degrees) * PI / 180.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 
"The sine value of 90 degrees is " + Math.sin(90 * Math.PI / 180);
</script>
</body>
</html>

Result:

JavaScript Math.sin()

Math.sin(x) returns the sin of x (given in radians):

Angle in radians = (angle in degrees) * PI / 180.

The sine value of 90 degrees is 1

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

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.cos()</h2>
<p>Math.cos(x) returns the cosine of x (given in radians):</p>
<p>Angle in radians = (angle in degrees) * PI / 180.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 
"The cosine value of 0 degrees is " + Math.cos(0 * Math.PI / 180);
</script>
</body>
</html>

Result:

JavaScript Math.cos()

Math.cos(x) returns the cosine of x (given in radians):

Angle in radians = (angle in degrees) * PI / 180.

The cosine value of 0 degrees is 1

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

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.min()</h2>
<p>Math.min() returns the lowest value in a list of arguments:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.min(0, 150, 30, 20, -8, -200);
</script>
</body>
</html>

Result:

JavaScript Math.min()

Math.min() returns the lowest value in a list of arguments:

-200

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.max()</h2>
<p>Math.max() returns the highest value in a list of arguments.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.max(0, 150, 30, 20, -8, -200);
</script>
</body>
</html>

Result:

JavaScript Math.max()

Math.max() returns the highest value in a list of arguments.

150

Math.random()

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Math.random() returns a random number between 0 and 1:</p>
<p id="demo"></p>
<p>Tip: Click on "Run" several times.</p>
<script>
document.getElementById("demo").innerHTML = Math.random();
</script>
</body>
</html>

Result:

JavaScript Math.random()

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

0.45338299318663067

Tip: Click on “Run” several times.

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

The Math.log() Method

Math.log(x) returns the natural logarithm of x.

The natural logarithm returns the time needed to reach a certain level of growth:

Examples:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(1);
</script>
</body>
</html>

Result:

JavaScript Math.log()

Math.log() returns the natural logarithm of a number:

0

Examples:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(2);
</script>
</body>
</html>

Result:

JavaScript Math.log()

Math.log() returns the natural logarithm of a number:

0.6931471805599453

Examples:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(3);
</script>
</body>
</html>

Result:

JavaScript Math.log()

Math.log() returns the natural logarithm of a number:

1.0986122886681096

Math.E and Math.log() are twins.

How many times must we multiply Math.E to get 10?

Examples:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>How many times must we multiply Math.E to get 10?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(10);
</script>
</body>
</html>

Result:

JavaScript Math.log()

How many times must we multiply Math.E to get 10?

2.302585092994046

The Math.log2() Method

Math.log2(x) returns the base 2 logarithm of x.

How many times must we multiply 2 to get 8?

Examples:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log2()</h2>
<p>Math.log2() returns the base 2 logarithm of a number.</p>
<p>How many times must we multiply 2 to get 8?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log2(8);
</script>
</body>
</html>

Result:

JavaScript Math.log2()

Math.log2() returns the base 2 logarithm of a number.

How many times must we multiply 2 to get 8?

3

The Math.log10() Method

Math.log10(x) returns the base 10 logarithm of x.

How many times must we multiply 10 to get 1000?

Examples:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log10()</h2>
<p>Math.log10() returns the base 10 logarithm of a number.</p>
<p>How many times must we multiply 10 to get 1000?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log10(1000);
</script>
</body>
</html>

Result:

JavaScript Math.log10()

Math.log10() returns the base 10 logarithm of a number.

How many times must we multiply 10 to get 1000?

3

JavaScript Math Methods

MethodDescription
abs(x)Returns the absolute value of x
acos(x)Returns the arccosine of x, in radians
acosh(x)Returns the hyperbolic arccosine of x
asin(x)Returns the arcsine of x, in radians
asinh(x)Returns the hyperbolic arcsine of x
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
atanh(x)Returns the hyperbolic arctangent of x
cbrt(x)Returns the cubic root of x
ceil(x)Returns x, rounded upwards to the nearest integer
cos(x)Returns the cosine of x (x is in radians)
cosh(x)Returns the hyperbolic cosine of x
exp(x)Returns the value of Ex
floor(x)Returns x, rounded downwards to the 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)Rounds x to the nearest integer
sign(x)Returns if x is negative, null or positive (-1, 0, 1)
sin(x)Returns the sine of x (x is in radians)
sinh(x)Returns the hyperbolic sine of x
sqrt(x)Returns the square root of x
tan(x)Returns the tangent of an angle
tanh(x)Returns the hyperbolic tangent of a number
trunc(x)Returns the integer part of a number (x)

Leave a Reply

Your email address will not be published. Required fields are marked *