JavaScript Math atan2()

The JavaScript Math.atan2() function returns the arctangent of the quotient of its arguments.

The syntax of the Math.atan2() function is:

Math.atan2(y, x)

atan2(), being a static method, is called using the Math class name.


Math.atan2() Parameters

The Math.atan2() function takes in:

  • y - The Y-coordinate of the point
  • x - The X-coordinate of the point

Return value from Math.atan2()

  • Returns the angle in radians between and π, formed between the positive X-axis and the line joining (0, 0) and (x, y).
  • Returns NaN for non-numeric arguments.

Example: Using Math.atan2()

var num = Math.atan2(1, 1);
console.log(num); // 0.7853981633974483 (PI/4)

var num = Math.atan2(4, 3);
console.log(num); // 0.9272952180016122

var num = Math.atan2(0, 5);
console.log(num); // 0

var num = Math.atan2(Infinity, 0);
console.log(num); // 1.5707963267948966 (PI/2)

var num = Math.atan2(-Infinity, 0);
console.log(num); // -1.5707963267948966 (-PI/2)

var num = Math.atan2(Infinity, -Infinity);
console.log(num); // 2.356194490192345 (3*PI/4)

Output

0.7853981633974483
0.9272952180016122
0
1.5707963267948966
-1.5707963267948966
2.356194490192345

Recommended readings: