Home JavaScript Math Methods atan2() JavaScript Math.atan2() Method Definition Math.atan2(y, x) returns the angle between the positive x-axis and the point (x, y). The angle is in radians , from -π to π.
Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().
Syntax Call Math.atan2() like this. Start with this line.
Call What it does Math.atan2(y, x)Returns the angle of the point (x, y), in radians.
Built into JavaScript. Call Math.atan2() on the Math object — no import required.
Library declarations interface Math {
atan2(y: number, x: number): number;
} Parameters Parameter Description y Required. The y coordinate of the point. Note: y comes first. x Required. The x coordinate of the point.
Return Value Return Value Description number The angle in radians, from -π to π. Returns NaN if either argument is not a number.
JavaScript Examples 1. Basic use 2. Angle of a point in degrees 3. Direction between two points Example 1 — Basic Use Pass y first, then x. Points in different quadrants give different angles.
console.log(Math.atan2(1, 1)); // 0.7853981633974483
console.log(Math.atan2(1, -1)); // 2.356194490192345
console.log(Math.atan2(-1, -1)); // -2.356194490192345
console.log(Math.atan2(0, -1)); // 3.141592653589793 0.7853981633974483
2.356194490192345
-2.356194490192345
3.141592653589793 How It Works Top right. (1, 1) gives π / 4, about 0.7854.Top left. (-1, 1) is written as atan2(1, -1) and gives 2.3562.Bottom left. atan2(-1, -1) gives a negative angle, -2.3562.On the axis. A point on the negative x-axis gives π.Example 2 — Angle of a Point in Degrees Find the angle of the point (3, 4) and convert it to degrees.
var x = 3;
var y = 4;
var radians = Math.atan2(y, x);
var degrees = radians * 180 / Math.PI;
console.log(radians.toFixed(4)); // 0.9273
console.log(degrees.toFixed(1)); // 53.1 How It Works Call atan2. Pass y first (4), then x (3).Convert. Multiply the radians by 180 / Math.PI.Result. The point is about 53.1 degrees from the x-axis.Example 3 — Direction Between Two Points Find the direction from one point to another. Wrap negative angles into the 0 to 360 range.
var from = { x: 5, y: 5 };
var to = { x: 2, y: 2 };
var dx = to.x - from.x;
var dy = to.y - from.y;
var degrees = Math.atan2(dy, dx) * 180 / Math.PI;
console.log(degrees); // -135
console.log((degrees + 360) % 360); // 225 How It Works Subtract. dx and dy are the distance moved on each axis: both -3.Get the angle. Math.atan2(dy, dx) gives -135 degrees.Wrap to 0-360. Add 360 and use % 360 to get 225.Compatibility
Browser & Runtime Support Math.atan2() has been part of JavaScript since ES1 and is Baseline Widely available everywhere.
✓ Baseline · ES1
Math.atan2() Supported in every browser and Node.js version. No polyfill is needed.
99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support Math.atan2 Excellent
Bottom line: Use Math.atan2() freely. It works in all browsers and runtimes.
Frequently Asked Questions What does Math.atan2() do? Math.atan2(y, x) returns the angle, in radians, between the positive x-axis and the point (x, y). It works in all four quadrants.
Why is the order y first, then x? This follows the math convention: atan2(y, x) is the angle whose tangent is y / x. Remember it as "y over x".
How do I get degrees from Math.atan2()? Multiply by 180 / Math.PI. To get 0 to 360 instead of -180 to 180, add 360 when the result is negative.
How do I find the angle between two points? Subtract the coordinates: Math.atan2(y2 - y1, x2 - x1). This is the direction from the first point to the second.
What is the difference between atan() and atan2()? Math.atan(y / x) cannot tell some points apart, and fails when x is 0. Math.atan2(y, x) handles every case.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion