Math.atan2
Returns the angle in radians between the positive x-axis and the point (x, y)
Syntax
Math.atan2(y, x)Parameters
| Parameter | Type | Description |
|---|---|---|
| y | number | The y coordinate |
| x | number | The x coordinate |
Return Value
The angle in radians (between -PI and PI)
Examples
console.log(Math.atan2(1, 1)); // ~0.785 (PI/4)
console.log(Math.atan2(0, -1)); // ~3.14159 (PI)function angleBetween(x1: number, y1: number, x2: number, y2: number) {
return Math.atan2(y2 - y1, x2 - x1) * (180 / Math.PI);
}
console.log(angleBetween(0, 0, 1, 1)); // 45const angles = [[0, 1], [1, 0], [0, -1], [-1, 0]];
angles.forEach(([y, x]) =>
console.log(Math.atan2(y, x).toFixed(2))
);Understanding Math.atan2
The Math.atan2 method in JavaScript returns the angle in radians between the positive x-axis and the point (x, y). It belongs to the Math object and is one of the most widely used methods for working with math values in modern JavaScript and TypeScript applications.
The method signature is Math.atan2(y, x). It accepts 2 parameters: y, x. When called, it returns the angle in radians (between -pi and pi). Understanding when and how to use atan2() helps you write more expressive, readable code.
Common use cases for Math.atan2 include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like math-sin, math-cos, math-tan, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Math.atan2 is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Math Methods
Other methods in the Math object
Related Tools
More Math Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.