Course
Math
JavaScript Tutorial
This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.
Math Object
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.
Syntax
The syntax to call the properties and methods of Math are as follows
var pi_val = Math.PI; // Propertyvar sine_val = Math.sin(30); // Method
Math Properties
Here is a list of all the properties of Math and their description.
In the following sections, we will have a few examples to demonstrate the usage of Math properties.
Math Methods
Here is a list of the methods associated with Math object and their description
In the following sections, we will have a few examples to demonstrate the usage of the methods associated with Math.
Examples
Example: Math Properties
The example below demonstrates that each property of the Math object has a constant value.
Here, we have accessed the values of the 'E', 'LN2’, and 'PI', etc., properties.
<html><head> <title> JavaScript - Math object's properties </title></head><body> <p id = "output"> </p> <script> document.getElementById("output").innerHTML = "Math.E == " + Math.E + "<br>" + "Math.LN2 == " + Math.LN2 + "<br>" + "Math.LN10 == " + Math.LN10 + "<br>" + "Math.PI == " + Math.PI + "<br>"+ "Math.LOG2E == " + Math.LOG2E + "<br>" + "Math.LOG10E == " + Math.LOG10E; </script></body></html>
Output
After executing the above program, it returns the values of the provided Math properties.
Math.E == 2.718281828459045Math.LN2 == 0.6931471805599453Math.LN10 == 2.302585092994046Math.PI == 3.141592653589793Math.LOG2E == 1.4426950408889634Math.LOG10E == 0.4342944819032518
Example: Math ceil() method
Here, we are computing the JavaScript ceil() method to return the smallest larger integer value than the number passed as an argument. Here, the method returns 6 for the 5.9 value.
<html><head> <title> JavaScript - Math.ceil() method </title></head><body> <p id = "output"> </p> <script> let ans = Math.ceil(5.9); document.getElementById("output").innerHTML = "Math.ceil(5.9) = " + ans; </script></body></html>
Output
After executing the above program, it returns the result as 6.
Math.ceil(5.9) = 6
Example: Math max() method
The Math.max() method is used to get the maximum value among the arguments passed as an array.
Here, we have passed six arguments to the Math.max() object, and the method returns the maximum value from them.
<html><head> <title> JavaScript - Math.max() method </title></head><body> <p id = "output"> </p> <script> let ans = Math.max(100, 10, -5, 89, 201, 300); document.getElementById("output").innerHTML = "Math.max(100, 10, -5, 89, 201, 300) = " + ans + "<br>"; </script></body></html>
Output
After executing the above program, it returns 300 as maximum value.
Math.max(100, 10, -5, 89, 201, 300) = 300
Example: Math cos() method
The Math.cos() method returns the cosine value of the number passed as an argument. The cosine value of 0 is 1, which you can see in the output of the example below.
<html><head> <title> JavaScript - Math.cos() method </title></head><body> <p id = "output"> </p> <script> let ans = Math.cos(0); document.getElementById("output").innerHTML = "Math.cos(0) = " + ans; </script></body></html>
Output
If we execute the above program, it returns "1" as result.
Math.cos(0) = 1