JavaScript

# How to Use Math atan2() Method in JavaScript

JavaScript provides the built-in “Math” object for performing all types of mathematical operations including trigonometric operations such as “Math.sin”, “Math.cos”, “Math.atan”, “Math.atan2”, etc. The “Math.atan2()” is the static trigonometric method that computes the arctangent of a right triangle when its sides are known. It is majorly used in “signal processing”, “embedded systems”, “power systems”, and “radar”.

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

Math.atan2(y, x)

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:

<h2>Math atan2() Method in JavaScript</h2>

<p>Math.atan2() returns the arctangent of the quotient of its arguments</p>

<p id="sample"></p>

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:

<script>

document.getElementById("sample").innerHTML = Math.atan2(9, 6);

</script>

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. 