Description
Math Object is used to perform the mathematical operations using properties and methods. Math object's static methods and properties can be called without creating an object. For example,
var pi_value = Math.PI;
Examples
[html]
<!Doctype html>
<html>
<head>
<title>JavaScript Tutorials</title>
<script type="text/javascript">
//Printing pi and e values using math object
document.write("<br>",Math.PI);
document.write("<br>",Math.E);
//Finding square root of a number
var n = prompt("</br>Enter a number", "");
var answer = Math.sqrt(n);
document.write("The square root of " + n + " is " + answer);
</script>
</head>
<body>
<script type="text/javascript">
</script>
</body>
</html>
[/html]
Output
[c]3.141592653589793
2.718281828459045
The square root of 144 is 12[/c]
Methods
Method |
Description |
abs(x) |
Absolute value of x |
acos(x) |
arccosine of x |
asin(x) |
arcsine of x |
atan(x) |
arctangent of x |
ceil(x) |
Rounds upwards to nearest integer |
cos() |
cosine of x |
exp() |
exponent of x |
floor() |
Rounds downward to nearest integer |
sqrt(x) |
Finds square root of x |
random() |
Random number between 0 and 1 |
exp() |
exponent of x |
pow(x,y) |
Returns value of x to the power of y |
max() |
Returns maximum value |
min() |
Returns minimum value |