JavaScript Math.atan2() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

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.

JavaScript
Math.atan2(y, x)
CallWhat 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
JavaScript
Math.atan2(y, x)
JavaScript
interface Math {
  atan2(y: number, x: number): number;
}

Parameters

ParameterDescription
yRequired. The y coordinate of the point. Note: y comes first.
xRequired. The x coordinate of the point.

Return Value

Return ValueDescription
numberThe angle in radians, from -π to π. Returns NaN if either argument is not a number.

JavaScript Examples

Example 1 — Basic Use

Pass y first, then x. Points in different quadrants give different angles.

JavaScript
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
Try It Yourself

How It Works

  1. Top right. (1, 1) gives π / 4, about 0.7854.
  2. Top left. (-1, 1) is written as atan2(1, -1) and gives 2.3562.
  3. Bottom left. atan2(-1, -1) gives a negative angle, -2.3562.
  4. 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.

JavaScript
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
Try It Yourself

How It Works

  1. Call atan2. Pass y first (4), then x (3).
  2. Convert. Multiply the radians by 180 / Math.PI.
  3. 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.

JavaScript
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
Try It Yourself

How It Works

  1. Subtract. dx and dy are the distance moved on each axis: both -3.
  2. Get the angle. Math.atan2(dy, dx) gives -135 degrees.
  3. Wrap to 0-360. Add 360 and use % 360 to get 225.

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

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.
This follows the math convention: atan2(y, x) is the angle whose tangent is y / x. Remember it as "y over x".
Multiply by 180 / Math.PI. To get 0 to 360 instead of -180 to 180, add 360 when the result is negative.
Subtract the coordinates: Math.atan2(y2 - y1, x2 - x1). This is the direction from the first point to the second.
Math.atan(y / x) cannot tell some points apart, and fails when x is 0. Math.atan2(y, x) handles every case.

Next: atanh()

Learn how to find the inverse hyperbolic tangent.

atanh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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