Math Object in JavaScript with Examples

(Last Updated On: April 2, 2021)

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():

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():

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():

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():

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():

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():

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():

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():

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():

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():

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():

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():

Math Object

Recommended For You

About the Author: 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

%d bloggers like this: