JavaScript

Math Object in JavaScript with Examples

Math object:

Math Object in JavaScript– The math object is a JavaScript object. It consists of properties (or constants) and methods. All these properties and method can be called by using directly math object. Mostly its methods are used to perform different mathematical tasks.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Methods of math JavaScript object:

The most crucial and commonly used methods of math object are as follows:

Abs():

This method returns the absolute value of a give number. The general syntax to use this method is:

Math.abs(number);

For example

12.3 returns 12.3

-24.3 returns 24.3

3-2 returns 1


The following code demonstrates the function of Math.abs():

<html>
<head>

<script type="text/javaScript">

var a=12.3;
var b= -24.3;
var c=3-2;
document.write(Math.abs(a)+"<br>");
document.write(Math.abs(b)+"<br>");
document.write(Math.abs(c)+"<br>");

</script>

</head>
<body>

</body>
</html>

Math Object

Sqrt():

This method returns the square root of a given positive number. This method returns NaN if the given number is negative. The general syntax to use this method is:

Math.sqrt(number);

For example:

Math.sqrt(-4) returns NaN.

Math.sqrt(9) returns 3.

Math.sqrt(0.64) returns 0.7937.

Math.sqrt(1) returns 1.

The following code demonstrates the function of Math.sqrt():

<html>
<head>

<script type="text/javaScript">

var a=-4;
var b= 9;
var c=0.63;
var d= 1;
document.write(Math.sqrt(a)+"<br>");
document.write(Math.sqrt(b)+"<br>");
document.write(Math.sqrt(c)+"<br>");
document.write(Math.sqrt(d)+"<br>");


</script>

</head>
<body>

</body>
</html>

Math Object



Ceil():

This method returns the value of a given number rounded upwards to the nearest integer. The general syntax to use this method is:

Math.ceil(number);

Example:

Math.ceil(0.65) returns 1.

Math.ceil(0.40) returns 1.

Math.ceil(6.1) returns 7.

Math.ceil(-6.9) returns -6.

Math.ceil(-6.1) returns -6.

The following code demonstrates the function of Math.ceil():

<html>
<head>

<script type="text/javaScript">

var a=0.65;
var b= 0.40;
var c=6.1;
var d= -6.9;
var e= -6.1;
document.write(Math.ceil(a)+"<br>");
document.write(Math.ceil(b)+"<br>");
document.write(Math.ceil(c)+"<br>");
document.write(Math.ceil(d)+"<br>");
document.write(Math.ceil(e)+"<br>");


</script>

</head>
<body>

</body>
</html>

Math Object

Floor():

This method returns the value of a given number rounded downwards to the nearest integer. The general syntax to use this method is:

Math.floor(number);

Example:

Math.floor(0.65) returns 0.

Math.floor(0.40) returns 0.

Math.floor(6.1) returns 6.

Math.floor(-6.9) returns -7.

Math.floor(-6.1) returns -7.

The following code demonstrates the function of Math.floor():

<html>
<head>

<script type="text/javaScript">

var a=0.65;
var b= 0.40;
var c=6.1;
var d= -6.9;
var e= -6.1;
document.write(Math.floor(a)+"<br>");
document.write(Math.floor(b)+"<br>");
document.write(Math.floor(c)+"<br>");
document.write(Math.floor(d)+"<br>");
document.write(Math.floor(e)+"<br>");


</script>

</head>
<body>

</body>
</html>

Math Object


Pow():

This method is used to find the exponential power of a given number. The general syntax to use this method is:

Math.pow(x,y)

It returns the value of x to the power y ( xy).

Example:

Math.pow(2,2) returns 4.

Math.pow(3,2) returns 9.

Math.pow(-2,3) returns -8.

The following code demonstrates the function of Math.pow():

<html>
<head>

<script type="text/javaScript">


document.write(Math.pow(2,2)+"<br>");
document.write(Math.pow(3,2)+"<br>");
document.write(Math.pow(-2,3)+"<br>");



</script>

</head>
<body>

</body>
</html>

Math Object

Round():

This method rounds a number to the nearest integer. The general syntax to use this method is:

Math.round(number);

Example:

Math.round(0.51) returns 1.

Math.round(0.50) returns 1.

Math.round(0.49) returns 0.

Math.round(-3.55) returns -4.

The following code demonstrates the function of Math.round():

<html>
<head>

<script type="text/javaScript">


document.write(Math.round(0.51)+"<br>");
document.write(Math.round(0.50)+"<br>");
document.write(Math.round(0.49)+"<br>");
document.write(Math.round(-3.55)+"<br>");



</script>

</head>
<body>

</body>
</html>

Math Object


Random():

This method returns a random number between 0 and 1. The general syntax to use this method is:

Math.random();

To return a random number up to a specified number, multiply the number with this function. For example, to return a random number up to 1000, the statement is written as;

document.write(Math.random()*1000);

The following code demonstrates the function of Math.random():

<html>
<head>

<script type="text/javaScript">


document.write(Math.random()*1000+"<br>");

</script>

</head>
<body>

</body>
</html>

Math Object

Math Object

max(x,y):

this method is used to find the maximum number of two numbers. The general syntax to use this method is:

Math.max(x,y);

Example:

Math.max(6,7) returns 7.

Math.max(-3,-2) returns -2

Math.max(-2,3) returns 3

Math.max(5.25,5.26) returns 5.26


The following code demonstrates the function of Math.max():

<html>
<head>

<script type="text/javaScript">


document.write(Math.max(6,7)+"<br>");
document.write(Math.max(-3,-2)+"<br>");
document.write(Math.max(-2,3)+"<br>");
document.write(Math.max(5.25,5.26)+"<br>");

</script>

</head>
<body>

</body>
</html>

Math Object

Min(x,y):

This method is used to find the minimum number of two numbers. The general syntax to use this method is:

Math.min(x,y);

Example:

Math.max(6,7) returns 6.

Math.max(-3,-2) returns -3

Math.max(-2,3) returns -2

Math.max(5.25,5.26) returns 5.25

The following code demonstrates the function of Math.min():

<html>
<head>

<script type="text/javaScript">


document.write(Math.min(6,7)+"<br>");
document.write(Math.min(-3,-2)+"<br>");
document.write(Math.min(-2,3)+"<br>");
document.write(Math.min(5.25,5.26)+"<br>");

</script>

</head>
<body>

</body>
</html>

Math Object

Cos():

This method returns the cosine of a given number. It returns a numeric value between -1 and 1, which represents the cosine of the angle. The general syntax to use this method is:

Math.cos(x);

Example:

Math.cos(3) returns -0.98999.

Math.cos(0) returns 1.

Math.cos(1) returns 0.5403023.



The following code demonstrates the function of Math.cos():

<html>
<head>

<script type="text/javaScript">


document.write(Math.cos(3)+"<br>");
document.write(Math.cos(0)+"<br>");
document.write(Math.cos(1)+"<br>");


</script>

</head>
<body>

</body>
</html>

Math Object

Sine():

This method returns the sine of a given number. The general syntax to use this method is:

Math.sin(x);

Example:

Math.sin(1) returns 0.8417.

Math.sin(0) returns 0

The following code demonstrates the function of Math.sin():

<html>
<head>

<script type="text/javaScript">


document.write(Math.sin(1)+"<br>");
document.write(Math.sin(0)+"<br>");



</script>

</head>
<body>

</body>
</html>

Math Object


Tan():

This method returns the tangent of  given number. The general syntax to use this method is:

Math.tan(x);

Example:

Math.tan(1) returns 1.5574.

Math.tan(0) returns 0

The following code demonstrates the function of Math.tan():

<html>

<head>




<script type="text/javaScript">







document.write(Math.tan(1)+"<br>");

document.write(Math.tan(0)+"<br>");




</script>




</head>

<body>




</body>

</html>

Math Object

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

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

Back to top button