The JavaScript atan function is a Math function that is useful for calculating the trigonometric arc tangent for the specified expression. Arc Tangent is also called the inverse of a TANGENT function. The syntax of the atan Function is
Math.atan(number);
Return Value: The atan() function returns an angle in radians between -π/2 and π/2.
JavaScript atan Function Example
In this JavaScript example, we will find the Arc Tangent values of different data types and display the output.
For the Str element ID, we used the String Value, and it returns NaN (Not a Number). Next, we tried the Null Values in this Math function, which returns zero as output.
<!DOCTYPE html>
<html>
<head>
<title> JavaScriptATANFunction </title>
</head>
<body>
<h1> JavaScriptATANFunction </h1>
<p id = "Pos"></p>
<p id = "Neg"></p>
<p id = "Dec"></p>
<p id = "Neg_Dec"></p>
<p id = "Str"></p>
<p id = "Exp"></p>
<p id = "Null"></p>
<p id = "Multi"></p>
<script>
document.getElementById("Pos").innerHTML = Math.atan(1);
document.getElementById("Neg").innerHTML = Math.atan(-1);
document.getElementById("Dec").innerHTML = Math.atan(0.45);
document.getElementById("Neg_Dec").innerHTML = Math.atan(-0.75);
document.getElementById("Str").innerHTML = Math.atan("JavaScript");
document.getElementById("Null").innerHTML = Math.atan(null);
document.getElementById("Multi").innerHTML = Math.atan(25 + 55 - 77);
</script>
</body>
</html>

Example 2: If we use the atan() function on positive infinity, it returns π/2, and for negative infinity, it returns -π/2. However, if we use it on NaN, the atan() method returns NaN as output.
let a = Infinity;
let b = -Infinity;
let c = NaN;
console.log(Math.atan(a));
console.log(Math.atan(b));
console.log(Math.atan(c));
1.5707963267948966
-1.5707963267948966
NaN
Example 3: In this example, we use the atan() function to find the arc tangent value of positive and negative 0.
console.log(Math.atan(0));
console.log(Math.atan(-0));
0
-0
Example 4: Using Math.atan() on non-numeric values or strings will return NaN.
let a = "Hi";
console.log(Math.atan(a));
NaN
TIP: Please refer to the tan(), atan2(), tanh(), and atanh() functions for finding the arc and hyperbolic tangent values.
JavaScript angle between two coordinates
JavaScript Math library has atan() and atan2(), but we must use atan2() to find the angle between two coordinates.
function calcAng2Points(x1, y1, x2, y2) {
const radians = Math.atan2(y2 - y1, x2 - x1);
const degrees = radians * 180 / Math.PI;
return degrees;
}
console.log(calcAng2Points(0, 0, 5, 5));
console.log(calcAng2Points(0, 0, -10, 0));
45
180