Favorites

JavaScript Math.atan2() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Math.atan2()
JavaScript Math Static methodThis method is supported by all modern browsers.
Updated 28th November 2026

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

const rad = Math.atan2(1, 1);
console.log(rad, rad * 180 / Math.PI);
console.log(Math.atan2(0, -1) * 180 / Math.PI);

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee