JavaScript atan2()Method
Example
Example 1 - atan2(y,x)
Specify a coordinate (x, y) with values (4,8), use the atan2() method to calculate the angle in radians between the coordinate and the X-axis, as in the following example:
Math.atan2(8,4);
The above example outputs:
1.1071487177940904
Definition and Usage
atan2() returns the plane angle (in radians) between the line segment from the origin (0,0) to the point (x,y) and the positive direction of the x-axis, that is,Math.atan2(y,x)。
Note:atan2() has two parameters, the y coordinate is passed as the first parameter, and the x coordinate is passed as the second parameter.
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the atan2() method.
Syntax
Math.atan2(y,x)
| Parameters | Description |
|---|---|
| y | Required. A number representing the Y coordinate. |
| x | Required. A number representing the x coordinate. |
Return Value
| Type | Description |
|---|---|
| Number | The arctangent value of x. Returns a numeric value between -PI and PI. Represents the offset angle corresponding to the point (x, y). This is a counterclockwise angle, in radians, between the positive X-axis and the line connecting the point (x, y) to the origin. Note that this function accepts parameters: pass the y coordinate first, then the x coordinate. |
Technical Details
| JavaScript Version: | 1.0 |
|---|
More Examples
Example
Math.atan2(90, 15) // 1.4056476493802699
Math.atan2(15, 90) // 0.16514867741462683
Math.atan2( ±0, -0 ) // ±PI.
Math.atan2( ±0, +0 ) // ±0.
Math.atan2( ±0, -x ) // ±PI for x > 0.
Math.atan2( ±0, x ) // ±0 for x > 0.
Math.atan2( -y, ±0 ) // -PI/2 for y > 0.
Math.atan2( y, ±0 ) // PI/2 for y > 0.
Math.atan2( ±y, -Infinity ) // ±PI for finite y > 0.
Math.atan2( ±y, +Infinity ) // ±0 for finite y > 0.
Math.atan2( ±Infinity, x ) // ±PI/2 for finite x.
Math.atan2( ±Infinity, -Infinity ) // ±3*PI/4.
Math.atan2( ±Infinity, +Infinity ) // ±PI/4.
Math.atan2(15, 90) // 0.16514867741462683
Math.atan2( ±0, -0 ) // ±PI.
Math.atan2( ±0, +0 ) // ±0.
Math.atan2( ±0, -x ) // ±PI for x > 0.
Math.atan2( ±0, x ) // ±0 for x > 0.
Math.atan2( -y, ±0 ) // -PI/2 for y > 0.
Math.atan2( y, ±0 ) // PI/2 for y > 0.
Math.atan2( ±y, -Infinity ) // ±PI for finite y > 0.
Math.atan2( ±y, +Infinity ) // ±0 for finite y > 0.
Math.atan2( ±Infinity, x ) // ±PI/2 for finite x.
Math.atan2( ±Infinity, -Infinity ) // ±3*PI/4.
Math.atan2( ±Infinity, +Infinity ) // ±PI/4.
Other Extensions
JavaScript Math Object