JavaScript Math.atan() Method
The JavaScript Math.atan() method accepts a number as a parameter and calculates the arctangent (inverse of tangent) of a number. The result is the angle (in radians) whose tangent is the specified number.
Here are different scenarios illustrating the behavior of the Math.atan() method −
- If the argument provided to this method is a numeric value, it returns the angle in radians between -/2 and /2.
- If the argument is "Inifinity", it returns /2.
- If it is "-Infinity", it returns -/2.
- If the argument is non-numeric or empty number, it returns "NaN" (Not a Number) as the result.
Syntax
Following is the syntax of JavaScript Math.atan() method −
Math.atan(x);
Parameters
This method accepts only one parameter. The same is described below −
- x: A number representing the tangent of an angle.
Return value
This method returns the arctangent (inverse of tangent) of the provided number in "radians".
Example 1
In the following example, we are demonstrating the basic usage of JavaScript Math.atan() method −
<html> <body> <script> let number1 = Math.atan(-2); document.write(number1, "<br>"); let number2 = Math.atan(0); document.write(number2, "<br>"); let number3 = Math.atan(2); document.write(number3); </script> </body> </html>
Output
It returns the arctangent (in radians) for the provided arguments.
Example 2
Here, we are calculating the arctangent of Infinity −
<html> <body> <script> let number1 = Math.atan(Infinity); document.write(number1, "<br>"); let number2 = Math.atan(-Infinity); document.write(number2); </script> </body> </html>
Output
As we can see, it returns /2 for "Infinity" and -/2 for "-Infinity".
Example 3
If we pass an empty number or non-numeric value as an argument to this method, it returns "NaN" as output −
<html>
<body>
<script>
let number1 = Math.atan("Tutorialspoint");
let number2 = Math.atan();
document.write(number1, "<br>", number2);
</script>
</body>
</html>
Output
If we execute the above program, it returns "NaN" as result.