Easy Tutorial
❮ Prop Reset Disabled Prop Win Innerheight Innerwidth ❯

JavaScript atan2() Method

JavaScript Math Object

Example

Example 1 - atan2(y,x)

Specify a coordinate (x, y), with the values (4, 8), and use the atan2() method to calculate the angle in radians between the coordinate and the X-axis, as shown in the example below:

Math.atan2(8,4);

The output of the above example is:

1.1071487177940904

Try it yourself »


Definition and Usage

The atan2() method returns the angle in radians between the line segment from the origin (0,0) to the point (x,y) and the positive x-axis, which is Math.atan2(y,x).

Note: The atan2() method takes two parameters, with the y-coordinate passed as the first parameter and the x-coordinate passed as the second parameter.


Browser Support

All major browsers support the atan2() method.


Syntax

Parameter Description
y Required. A number representing the Y-coordinate.
x Required. A number representing the X-coordinate.

Return Value

Type Description
Number The arctangent of x. Returns a value between -PI and PI, representing the angle of the point (x, y) relative to the origin. This is a counterclockwise angle, in radians, between the positive X-axis and the line connecting the origin to the point (x, y). Note that this function takes the y-coordinate first, followed by the x-coordinate.

Technical Details

| JavaScript Version: | 1.0 | | --- | --- |

More Examples

Example

Math.atan2(90, 15) // 1.4056476493802699
Math.atan2(15, 90) // 0.16514867741462683

Math.atan2( ±0, -0 )               // ±PI.
Math.atan2( ±0, +0 )               // ±0.
Math.atan2( ±0, -x )               // ±PI for x > 0.
Math.atan2( ±0, x )                // ±0 for x > 0.
Math.atan2( -y, ±0 )               // -PI/2 for y > 0.
Math.atan2( y, ±0 )                // PI/2 for y > 0.
Math.atan2( ±y, -Infinity )        // ±PI for finite y > 0.
Math.atan2( ±y, +Infinity )        // ±0 for finite y > 0.
Math.atan2( ±Infinity, x )         // ±PI/2 for finite x.
Math.atan2( ±Infinity, -Infinity ) // ±3*PI/4.
Math.atan2( ±Infinity, +Infinity ) // ±PI/4.

JavaScript Math Object

❮ Prop Reset Disabled Prop Win Innerheight Innerwidth ❯