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.

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

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

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.

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.

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.

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.

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.

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);

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

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

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.

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

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