JavaScript Math.atan2()
Example - atan2(y,x)
Assume you had a point with the (x,y) coordinates of (4,8), you could calculate the angle in radians between that point and the positive X axis as follows:
Math.atan2(8, 4);
Description
The Math.atan2()
method returns the arctangent of the quotient of its arguments, as a numeric value between PI and -PI radians.
The number returned represents the counterclockwise angle in radians (not degrees) between the positive X axis and the point (x, y).
Note: With atan2(), the y coordinate is passed as the first argument and the x coordinate is passed as the second argument.
JavaScript Tangent Methods:
The Math.tan() MethodThe Math.tanh() Method
The Math.atan() Method
The Math.atan2() Method
The Math.atanh() Method
Sine and Cosine Methods:
The Math.sin() MethodThe Math.cos() Method
Syntax
Math.atan2(y, x)
Parameters
Parameter | Description |
y | Required. The y coordinate. |
x | Required. The x coordinate. |
Return Value
Type | Description |
Number | PI to -PI.NaN if the values are empty or not numeric. |
Browser Support
Math.atan2()
is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |