The atan() method calculates the arctangent (inverse of tangent) of the specified angle and returns it.
Example
atan() Syntax
The syntax of the atan() method is:
Math.atan(angle)
Here, atan() is a static method. Hence, we are accessing the method using the class name, Math.
atan() Parameter
The atan() method takes a single parameter:
angle- in radians whose arctangent value is calculated
atan() Return Value
The atan() method returns:
- arctangent value of the
angleargument - NaN (Not a Number) if
xis a non-numeric value
Note: The returned angle will always be in the range -π/2 to π/2 for numeric arguments.
Example 1: JavaScript Math.atan()
In the above example,
Math.atan(0)- calculates the arctangent of0Math.atan(-5)- calculates the arctangent of-5
Example 2: Math.atan() with Infinity
Here the math.atan() method calculates the arctangent of infinity. As you can see, the output value is still between -π/2 and π/2.
Example 3: Math.atan() with a Non-Numeric Argument
In the program above, we have used a string argument, Dwayne with atan(). In this case, we get NaN as output.
Recommended readings: