JavaScript Math.atan2() Method
The Math.atan2() static method returns the angle from the x axis to a point, which is useful for finding the direction between two points.
Description
The Math.atan2() static method returns the angle, in radians, between the positive x axis and the point (x, y). Note that y comes first. Unlike Math.atan(y / x), it works out the correct quadrant and handles x = 0. It's often used to rotate something so it points at the mouse.
The result is in radians, from -π to π, measured anticlockwise from the positive x axis in maths conventions. In screen coordinates, where y increases downwards, angles appear to turn clockwise instead, so you may need to flip the sign of y when converting.
Converting the result to degrees, angle * 180 / Math.PI, makes it easy to use with CSS rotate() transforms, such as rotating an arrow to follow the pointer. Like every member of Math, it's called on the Math object itself rather than on a number, and it only works with the Number type: passing a BigInt throws a TypeError.
Syntax
Math.atan2(y, x)Parameters
- yThe y coordinate.
- xThe x coordinate.
Return Value
The angle in radians, from -π to π, between the positive x axis and the point (x, y).
Example
Browser Support
The following information will show you the current browser support for the Math.atan2() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





