The atan()
method calculates the arctangent (inverse of tangent) of the specified angle and returns it.
Example
let num = Math.atan(1);
console.log(num);
//Output: 0.7853981633974483
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
angle
argument - NaN (Not a Number) if
x
is 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()
// compute arctangent of 0
let number1 = Math.atan(0);
console.log(number1);
// compute arctangent of -5
let number2 = Math.atan(-5);
console.log(number2);
// Output:
// 0
// -1.373400766945016
In the above example,
Math.atan(0)
- calculates the arctangent of0
Math.atan(-5)
- calculates the arctangent of-5
Example 2: Math.atan() with Infinity
// atan() with positive infinity
let number1 = Math.atan(Infinity);
console.log(number1);
// atan() with negative infinity
let number2 = Math.atan(-Infinity);
console.log(number2);
// Output:
// 1.5707963267948966 (π/2)
// -1.5707963267948966 (-π/2)
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
// string variables
let string = "Dwayne"
// atan() with string arguments
let result = Math.atan(string);
console.log(result);
// Output: NaN
In the program above, we have used a string argument, Dwayne
with atan()
. In this case, we get NaN as output.
Recommended readings: