Computer >> Computer tutorials >  >> Programming >> Javascript

What is math object in JavaScript?


The math object has 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(60);

You can try to run the following code to learn how to implement math object in JavaScript. Here, we’re working on Math PI property to return the ratio of the circumference of a circle to its diameter which is approximately 3.14159 −

Example

<html>
   <head>
      <title>JavaScript Math PI Property</title>
   </head>
   <body>
      <script>
         var property_value = Math.PI
         document.write("Property Value (PI) : " + property_value);
      </script>
   </body>
</html>

Output

Property Value (PI) : 3.141592653589793