Java Script


JavaScript Tutorial


Admission Enquiry Form


Math object in JavaScript

In JavaScript, object is the container of properties/variables and methods/functions. Math is the object which contains some methods to perform some standard mathematical functions.

Math properties and methods in JavaScript.

Example of Math Properties in JavaScript

document.write(Math.SQRT2+"<br>"); // returns the square root of 2
document.write(Math.SQRT1_2+"<br>"); // returns the square root of 1/2
document.write(Math.E+"<br>"); // returns Euler's number
document.write(Math.PI); // returns PI



Output:

The code above will display :

1.4142135623730951
0.7071067811865476
2.718281828459045
3.141592653589793



Example of Math.sqrt() method

This method returns the square root of the given number

document.write(Math.sqrt(144));



Output:

The code above will display :

12




Example of Math.abs() method

This method returns the absolute value of the number

document.write(Math.abs(-20));



Output:

The code above will display :

20




Example of Math.min() method

This method returns the minimum value of the given values

document.write(Math.min(4,0,250,-3,50));



Output:

The code above will display :

-3




Example of Math.max() method

This method returns the maximum value of the given values

document.write(Math.max(4,0,250,-3,50));



Output:

The code above will display :

250




Example of Math.trunc() method

This method returns the integer part of the number

document.write(Math.trunc(4.34));



Output:

The code above will display :

4




Example of Math.random() method

This method returns a random number between 0 and 1

document.write(Math.random());



Output:

The code above will display :

0.904131097411031




Another Example of Math.random() method

This code using Math.random() method to get a random value between 0 to 9

document.write(Math.trunc(Math.random()*10));



Output:

The code above will display :

7




Example of Math.log() method

This method returns the log of given number

document.write(Math.log(1));



Output:

The code above will display :

0




Example of Math.round() method

This method returns the nearest integer of the given number

document.write(Math.round(2.7));



Output:

The code above will display :

3




Example of Math.pow() method

This method returns the power of the given number

document.write(Math.pow(2,8));



Output:

The code above will display :

256




Example of Math.ceil() method

This method returns the upward value to its nearest integer.

document.write(Math.ceil(4.3));



Output:

The code above will display :

5




Example of Math.floor() method

This method returns the downward value to its nearest integer.

document.write(Math.floor(4.3));



Output:

The code above will display :

4


Example of Math.sign() method

This method finds that, if the number is positive, negative or zero

document.write(Math.sign(-43)+"<br>");
document.write(Math.sign(0)+"<br>");
document.write(Math.sign(4));



Output:

The code above will display :

-1
0
1