JavaScript Math.atan2()
Syntax & Examples
Math.atan2() static-method
The Math.atan2() method in JavaScript returns the arctangent of the quotient of its arguments, which represents the angle θ of the point (x, y) in the plane. The method returns a value in radians, which is between -π and π (inclusive).
Syntax of Math.atan2()
The syntax of Math.atan2() static-method is:
Math.atan2(y, x)
This atan2() static-method of Math returns the arctangent of the quotient of its arguments.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
y | required | A number representing the y-coordinate of a point. |
x | required | A number representing the x-coordinate of a point. |
Return Type
Math.atan2() returns value of type Number
.
✐ Examples
1 Using Math.atan2() with positive coordinates
In JavaScript, we can use the Math.atan2()
method to get the arctangent of the quotient of its arguments (y, x) for positive coordinates.
For example,
- We call the
Math.atan2()
method with positive coordinatesy
andx
. - The arctangent value is returned and stored in the variable
result
. - We log
result
to the console usingconsole.log()
method.
JavaScript Program
const y = 1;
const x = 1;
const result = Math.atan2(y, x);
console.log(result);
Output
0.7853981633974483
2 Using Math.atan2() with negative y-coordinate
In JavaScript, we can use the Math.atan2()
method to get the arctangent of the quotient of its arguments (y, x) for a negative y-coordinate.
For example,
- We call the
Math.atan2()
method withy
as -1 andx
as 1. - The arctangent value is returned and stored in the variable
result
. - We log
result
to the console usingconsole.log()
method.
JavaScript Program
const y = -1;
const x = 1;
const result = Math.atan2(y, x);
console.log(result);
Output
-0.7853981633974483
3 Using Math.atan2() with negative x-coordinate
In JavaScript, we can use the Math.atan2()
method to get the arctangent of the quotient of its arguments (y, x) for a negative x-coordinate.
For example,
- We call the
Math.atan2()
method withy
as 1 andx
as -1. - The arctangent value is returned and stored in the variable
result
. - We log
result
to the console usingconsole.log()
method.
JavaScript Program
const y = 1;
const x = -1;
const result = Math.atan2(y, x);
console.log(result);
Output
2.356194490192345
Summary
In this JavaScript tutorial, we learned about atan2() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.