JavaScript Tutorial JavaScript Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

JavaScript - Math.atan2() Method



The JavaScript Math.atan2() method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). The return value will be in the range -𝜋 through 𝜋.

Syntax

Math.atan2(y, x);

Parameters

y Specify the ordinate coordinate.
x Specify the abscissa coordinate.

Return Value

Returns theta of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.

Example:

In the example below, Math.atan2() method is used to calculate the theta of a point.

var txt;

txt = "Math.atan2(10, 10) = " + Math.atan2(10, 10) + "<br>";
txt = txt + "Math.atan2(20, 10) = " + Math.atan2(20, 10) + "<br>";
txt = txt + "Math.atan2(-20, 10) = " + Math.atan2(-20, 10) + "<br>";

The output (value of txt) after running above script will be:

Math.atan2(10, 10) = 0.7853981633974483
Math.atan2(20, 10) = 1.1071487177940904
Math.atan2(-20, 10) = -1.1071487177940904

❮ JavaScript - Math Object

5