# Introduction JavaScript Math Atan2() Function with Examples

Math.atan2() is a function in math library of JavaScript that is used to find the arctangent of the quotient of its arguments. The method will return the numeric value which is the angle of (x,y) point and x-axis.

Math.atan2() is a function in math library of JavaScript that is used to find the arctangent of the quotient of its arguments. The method will return the numeric value which is the angle of (x,y) point and x-axis.

### Syntax

Its syntax is as follows

``````Math.atan2(y,x);

``````

### Parameter(s)

• y,x – represent the y and x co-ordinates on the point.

### Return Value

Returns the arctangent in radians of a number.

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

``````
Math.atan2() Method with Example

The Math.atan2() method returns a numeric value between -π and π representing the angle theta of an (x, y) point. This is the counterclockwise angle, measured in radians, between the positive X axis, and the point (x, y). Note that the arguments to this function pass the y-coordinate first and the x-coordinate second.

Math.atan2() is passed separate x and y arguments, and Math.atan() is passed the ratio of those two arguments.

Because atan2() is a static method of Math, you always use it as Math.atan2(), rather than as a method of a Math object you created (Math is not a constructor).

### Example 1

The following example demonstrates the case where an empty value is passed.

``````
//example1.js

var x;
var y;

console.log(Math.atan2(x, y));
console.log(Math.atan2());

``````

Output

``````node example1
NaN
NaN

``````

### Example 2

The following example demonstrates the use of the Javascript atan2() method.

``````//example2.js

var x = 5;
var y = 4;

console.log(Math.atan2(y, x));

var x = 90;
var y = 15;

console.log(Math.atan2(y, x));

var x = 15;
var y = 90;

console.log(Math.atan2(y, x));

``````

Output

``````
node example2
0.6747409422235527
0.16514867741462683
1.4056476493802699

``````

### Example 3

The following example demonstrates the following case:

If the second parameter is passed as negative zero, and the first argument is ±0, the method returns pi with the same sign as the first argument.

``````
//example3.js

var x = -0;
var a = 0;
var b = -0;

console.log(Math.atan2(a, x));
console.log(Math.atan2(b, x));

``````

Output

``````node example3
3.141592653589793
-3.141592653589793

``````

### Example 4

The following example demonstrates the case:

If the first parameter is passed as ±0, and the second argument is negative, the method returns pi with the same sign as the first argument.

``````//example4.js

var x = -5;
var a = 0;
var b = -0;

console.log(Math.atan2(a, x));
console.log(Math.atan2(b, x));

``````

Output

``````node example4
3.141592653589793
-3.141592653589793

``````

### Example 5

The following example demonstrates the case:

If the first parameter is passed as ±0, and the second argument is positive, the method returns 0 with a same sign as the first argument.

``````//example5.js

var x = 5;
var a = 0;
var b =- 0;

console.log(Math.atan2(a, x));
console.log(Math.atan2(b, x));

``````

Output

``````
node example6
0
-0

``````

### Example 6

The following example demonstrates the case:

If the first parameter is passed as infinite, then for the second argument is negative infinity, the method returns 3*pi/4 with the same sign as the first argument, and for the second argument is positive infinity, the method returns pi/4 with the same sign as the first argument.

``````
// example6.js

var x = Number.POSITIVE_INFINITY;
var y = Number.NEGATIVE_INFINITY;

console.log(Math.atan2(x, y));
console.log(Math.atan2(y, y));

console.log(Math.atan2(x, x));
console.log(Math.atan2(y, x));

``````

Output

``````node example6
2.356194490192345
-2.356194490192345
0.7853981633974483
-0.7853981633974483

``````