The atan function in JavaScript computes the arctangent of a specified number and returns the result in radians. The output provided by the atan method ranges from -Math.PI/2 to Math.PI/2.
Syntax
The atan function is expressed using the syntax outlined below:
Example
Math.atan(num)
Parameter
num - A number.
Return
The arctangent of the number.
JavaScript Math atan method example
In this section, we will explore the atan function by examining a variety of examples.
Example 1
Let's see a simple example of atan method.
Example
<script>
document.writeln(Math.atan(-1)+"<br>");
document.writeln(Math.atan(0)+"<br>");
document.writeln(Math.atan(0.5)+"<br>");
document.writeln(Math.atan(1));
</script>
Output:
Output
-0.7853981633974483
0
0.4636476090008061
0.7853981633974483
Example 2
Consider an example where the atan function is utilized with infinity as its input argument.
Example
<script>
document.writeln(Math.atan(Infinity)+"<br>");
document.writeln(Math.atan(-Infinity));
</script>
Output:
Output
1.5707963267948966
-1.5707963267948966
Example 3
In this section, you have the opportunity to evaluate the atan function using your own specific test cases.
Example
<script>
function display()
{
var x=document.getElementById("num").value;
document.getElementById("result").innerHTML=Math.atan(x);
}
</script>
<form>
Enter a number: <input type="text" id="num">
<input type="button" onclick="display()" value="submit">
</form>
<p><span id="result"></span></p>