JavaScript

# Math Objects in JavaScript Porperties and methods with Examples

## Math objects in JavaScript:

math objects 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 method are used to perform different mathematical tasks.

### Methods of math objects:

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

Math Objects 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 Objects 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 Objects 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 ObjectsÂ  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 Objects 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 Objects 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 Objects 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 Objects 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 Objects 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 Objects 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():

## Related Article:

https://programmingdigest.com/string-objects-in-javascript-and-substring-objects-properties-and-methods/

Check Also
Close