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

ParameterOptional/RequiredDescription
yrequiredA number representing the y-coordinate of a point.
xrequiredA 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,

  1. We call the Math.atan2() method with positive coordinates y and x.
  2. The arctangent value is returned and stored in the variable result.
  3. We log result to the console using console.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,

  1. We call the Math.atan2() method with y as -1 and x as 1.
  2. The arctangent value is returned and stored in the variable result.
  3. We log result to the console using console.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,

  1. We call the Math.atan2() method with y as 1 and x as -1.
  2. The arctangent value is returned and stored in the variable result.
  3. We log result to the console using console.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.