JavaScript (ES2026) reference · Math Methods and Constants
JavaScript Math.atan2() Method
Return the angle in radians between the positive x axis and the point (x, y). Note the argument order: y first.
JavaScript (ES2026) Since ES1
Syntax
Math.atan2(y, x)
Parameters
| Parameter | Description |
|---|---|
y |
The y coordinate. |
x |
The x coordinate. |
Return value
An angle between -π and π.
Examples
const toDeg = (r) => (r * 180) / Math.PI;
console.log(toDeg(Math.atan2(1, 1)), toDeg(Math.atan2(1, -1)));
Output
45 135
const heading = (dx, dy) => (Math.round((Math.atan2(dy, dx) * 180) / Math.PI) + 360) % 360;
console.log(heading(0, 1), heading(-1, 0));
Output
90 180
Version history
| Version | Change |
|---|---|
| JavaScript ES1 | Added |
Related
Math.sin()· Return the sine of an angle given in radians. Convert degrees with deg * Math.PI / 180 first.Math.cos()· Return the cosine of an angle given in radians. Convert degrees with deg * Math.PI / 180 first.- JavaScript Math · tutorial
- JavaScript Numbers · tutorial