JavaScript Math atan2() is used to calculate the arctangent of the quotient of its arguments.
Syntax
Math.atan2(y,x)
Parameters
- y: The y coordinate of the point.
- x: The x coordinate of the point.
Return Value
The arctangent quotient is in the range from -pi to pi radians. It returns NaN for non-numeric arguments and empty.
Visual Representation
Example 1: How to Use Math atan2() Method
console.log(Math.atan2(10, 4)); //y-coordinate is greater than the x-coordinate
console.log(Math.atan2(3, 7)); //y-coordinate is less than the x-coordinate
console.log(Math.atan2(5, 5)); //y-coordinate is same as the x-coordinate
Output
1.1902899496825317
0.4048917862850834
0.7853981633974483
Example 2: Passing Negative value
console.log(Math.atan2(-10, 4));
console.log(Math.atan2(3, -7));
console.log(Math.atan2(-5, -5));
Output
-1.1902899496825317
2.7367008673047097
-2.356194490192345
Example 3:Passing an empty and Non-Numeric Argument
console.log(Math.atan2()); //empty value
console.log(Math.atan2("AppDividend")); //non-numeric value
Output
NaN
NaN
Example 4: Passing ±0 as an argument
console.log(Math.atan2(0, 4));
console.log(Math.atan2(4, 0));
console.log(Math.atan2(0, 0));
Output
0
1.5707963267948966
0
Example 5: Passing an Infinity
console.log(Math.atan2(Infinity, 0));
console.log(Math.atan2(-Infinity, 0));
console.log(Math.atan2(Infinity, -Infinity));
Output
1.5707963267948966
-1.5707963267948966
2.356194490192345
Browser compatibility
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Opera 3 and above
- Safari 1 and above