This post illustrates the working and usage of the “Math.atan2()” method in JavaScript.
How to Use the “Math atan2()” Method in JavaScript?
The “Math.atan2()” method calculates the angle “θ” in radians between the positive “y-axis” and the “x-axis”. It accepts the argument “y-axis” first and then the “x-axis”, and is represented as a point(y, x).
Syntax
In the above syntax:
- Math: Denotes the pre-defined “Math” object.
- atan2: Calculates the arctangent of two arguments which are the “y” and “x” axis, respectively.
Example: Applying the “Math atan2()” Method in JavaScript
In this example, the “Math.atan2()” method is used to find the counter clockwise angle between the positive “y” and “x” axis.
HTML Code
First, overview of the following HTML code:
In the above code snippet:
- The “<h2>” tag defines the subheading.
- The first “<p>” tag creates a paragraph with the stated message.
- The second “<p>” tag creates an empty paragraph assigned an id “sample” to display the value of the applied “Math.atan2()” method.
JavaScript Code
Next, move on to the JavaScript code:
In this code, the “document.getElementById()” method fetches the empty paragraph via its id “sample”. After that, it displays the calculated angle between the points “(9, 6)” with the help of the applied “Math.atan2()” method.
Output
The above output displays the computed arctangent of the specified points “(9, 6)” via the “Math atan2()” method.
Conclusion
JavaScript proposes the built-in “Math atan2()” method to compute the angle in radians between the “y-axis” and “x-axis” except for the signs. It comes from the pre-defined “Math” object properties that perform mathematical operations. This post illustrated the objective and working of the “Math atan2()” method in JavaScript.