JavaScript Math.atan2(y,x) Method
- Syntax of JavaScript
Math.atan2(y,x): - Example Code: Use the
Math.atan2(y,x)Method to Get the Angle of a Point - Example Code: Use the
Math.atan2(y,x)Method to Experiment and Compare the Angles of Two Points
The Math.atan2(y,x) method provides a value that is the angle in radians (not degrees). This method calculates the arctangent of x/y.
In JavaScript, atan2() is known as a static method of Math.
Syntax of JavaScript Math.atan2(y,x):
Math.atan2(y,x); Parameters
y | A number representing the y-coordinate will be considered the first argument. |
x | A number representing the x-coordinate will be considered the second argument. |
Return
This method returns the angle in radians after calculating the arctangent of x/y. It will return NaN when the required parameters are empty or non-numeric.
Example Code: Use the Math.atan2(y,x) Method to Get the Angle of a Point
To calculate the angle in radians of the point(x,y), we use the method Math.atan2(). The y-coordinate will be passed as the first argument and the x-coordinate as the second.
In the example below, we have used the Math.atan2(y,x) method to get the angle in radians of the point(5,10).
let atan2 = Math.atan2(10,5); console.log(atan2); Output:
1.1071487177940904 Example Code: Use the Math.atan2(y,x) Method to Experiment and Compare the Angles of Two Points
Since the angle of the radians depends on the arctangent of x/y, we will get the same angles when the arctangent of the arguments is equal.
We have created two points in this example, (2.5,4) and (5,8). We used the Math.atan2(y,x) method to compare the angles of the two points.
let atan = Math.atan2(4,2.5); console.log(atan); let atan2 = Math.atan2(8,5); console.log(atan2); Output:
1.0121970114513341 1.0121970114513341 The Math.atan2(y,x) method is supported in all browsers of the current version.
