The math object provides you properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All the properties and methods of Math are static and can be called by using Math as an object without creating it.
Thus, you refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where x is the method's argument.
The syntax to call the properties and methods of Math are as follows
var pi_val = Math.PI; var sine_val = Math.sin(30);
Here is a list of all the properties of Math and their description.
Sr.No | Property & Description | Example |
---|---|---|
1 |
E
Euler's constant and the base of natural logarithms, approximately 2.718. |
<script type="text/javascript"> var property_value = Math.E document.write("Property Value is :" + property_value); </script> |
2 |
PI
Ratio of the circumference of a circle to its diameter, approximately 3.14159. |
<script type="text/javascript"> var property_value = Math.PI document.write("Property Value is : " + property_value); </script> |
In the following sections, we will have a few examples to demonstrate the usage of Math properties.
Here is a list of the methods associated with Math object and their description
Sr.No | Method & Description | Example |
---|---|---|
1 |
abs()
Returns the absolute value of a number. |
<script type="text/javascript"> var value = Math.abs(-1); document.write("First Test Value : " + value); var value = Math.abs(null); document.write(" |
2 |
floor()
Returns the largest integer less than or equal to a number. |
<script type="text/javascript"> var value = Math.floor(10.3); document.write("First Test Value : " + value); var value = Math.floor(30.9); document.write(" |