The JavaScript Math is a built-in object that provides properties and methods for mathematical constants and functions to execute mathematical operations. It is not a function object, not a constructor.
The Math functions consist of methods and properties. Following is the list of methods used with the Math object:
1. Math.round()
This method provides the value of the given number to a rounded integer. It can be written as:
Math.round(x), where x is a number.
2. Math.pow()
It provides the value of x to the power of y. It can be written as:
Math.pow(x, y), where x is a base number and y is an exponent to the given base.
3. Math.sqrt()
It gives the square root of a given integer. It can be written as:
Math.sqrt(x), where x is a number.
4. Math.abs()
It provides the absolute i.e. positive value of a number. It can be written as:
Math.abs(x); where x is a number.
5. Math.ceil()
It gives a smaller number, which is greater or equal to the given integer. It can be written as:
Math.ceil(x); where x is a number
6. Math.floor()
It gives a larger number, which is lesser or equal to the given integer. It can be written as:
Math.floor(x); where x is a number.
7. Math.sin()
It provides a sine of the given number. It can be written as:
Math.sin(x); where x is a number.
8. Math.cos()
It provides cosine of the given number. It can be written as:
Math.cos(x); where x is a number
9. Math.min() and Math.max()
The min() method is used to display the lowest value of the given arguments. It can be written as:
Math.min(val1, val2………valn); where val1, val2………valn are numbers.
The max() method is used to display the highest value of the given arguments. It can be written as:
Math.max(val1, val2………valn); where val1, val2………valn are numbers.
10. Math.random()
It provides a random number between 0 and 1. It can be written as:
Math.random();
11. Math.acos()
It provides an arccosine of an integer. It can be written as:
Math.acos(x); where x is a number.
12. Math.asin()
It provides arcsine of an integer. It can be written as:
Math.asin(x); where x is a number.
Examples
Let us see few examples for the above some methods of JavaScript Math Functions:
Math.abs()
Code:
<!DOCTYPE html>
<html>
<body>
<p id="abs_demo"></p>
<script>
document.getElementById("abs_demo").innerHTML = Math.abs(-5.6);
</script>
</body>
</html>
Output:
Math.ceil()
Code:
<!DOCTYPE html>
<html>
<body>
<p id="ceil_demo"></p>
<script>
document.getElementById("ceil_demo").innerHTML = Math.ceil(7.8);
</script>
</body>
</html>
Output:
Math.floor()
Code:
<!DOCTYPE html>
<html>
<body>
<p id="floor_demo"></p>
<script>
document.getElementById("floor_demo").innerHTML = Math.floor(5.8);
</script>
</body>
</html>
Output:
Math.sin()
Code:
<html>
<body>
<script type = "text/javascript">
var value = Math.sin( 4.5 );
document.write("First Value : " + value );
var value = Math.sin( 90 );
document.write("<br />Second Value : " + value );
var value = Math.sin( Math.PI/2 );
document.write("<br />Third Value : " + value );
</script>
</body>
</html>
Output:
Math.cos()
Code:
<html>
<body>
<script type = "text/javascript">
var value = Math.cos(90);
document.write("First Value : " + value );
var value = Math.cos(-1);
document.write("<br />Second Value : " + value );
var value = Math.cos(2*Math.PI);
document.write("<br />Third Value : " + value );
</script>
</body>
</html>
Output:
Math.min() and Math.max()
Code:
<!DOCTYPE html>
<html>
<body>
Minimum Value: <p id="min_demo"></p>
Maximum Value: <p id="max_demo"></p>
<script>
document.getElementById("min_demo").innerHTML =
Math.min(40, 87, 55, 25, 78, 14);
document.getElementById("max_demo").innerHTML =
Math.max(50, 90, 55, 25, 78, 14);
</script>
</body>
</html>
Output:
Math.random()
Code:
<html>
<body>
<script type = "text/javascript">
var value = Math.random( );
document.write("First Value : " + value );
var value = Math.random( );
document.write("<br />Second Value : " + value );
var value = Math.random( );
document.write("<br />Third Value : " + value );
</script>
</body>
</html>
Output:
Math.acos()
Code:
<html>
<body>
<script type = "text/javascript">
var value1 = Math.acos(-1);
document.write("First Value : " + value1 );
var value2 = Math.acos(null);
document.write("<br />Second Value : " + value2 );
var value3 = Math.acos(30);
document.write("<br />Third Value : " + value3 );
var value4 = Math.acos("string");
document.write("<br />Fourth Value : " + value4 );
</script>
</body>
</html>
Output: