Categories

JavaScript atan(), atan2() & atanh() Methods

The JavaScript Math.atan() returns the arctangent of a number as a value between -PI/2 and PI/2 radians.

Syntax:

``Math.atan(x) ``

Example:

``````<!DOCTYPE html>
<html>
<body>

<button onclick="atanExample()">Try it</button>
<p id="example-atan"></p>

<script>
function atanExample() {
document.getElementById("example-atan").innerHTML = Math.atan(3);
}
</script>

</body>
</html>``````

Output:

Click the button to display the arctangent of the number 3

The JavaScript Math.atan2() returns the arctangent of the quotient of its arguments, as a numeric value between PI and -PI radians.

The number returned expresses the counterclockwise angle in radians (not degrees) between the positive X-axis and the point (y, x).

Syntax:

``Math.atan2(y, x) ``

With JavaScript Math.atan2(), the y coordinate is passed as the first argument and the x coordinate is passed as the second argument.

Example:

``````<!DOCTYPE html>
<html>
<body>

<button onclick="atan2Example()">Try it</button>
<p id="example-aran2"></p>

<script>
function atan2Example() {
document.getElementById("example-aran2").innerHTML = Math.atan2(6, 3);
}
</script>

</body>
</html>``````

Output:

Click the button to display the arctangent of 6/3

The JavaScript Math.atanh() returns the hyperbolic arctangent of a number.

Syntax:

``Math.atanh(x) ``

Example:

``````<!DOCTYPE html>
<html>
<body>

<button onclick="atanhExample()">Try it</button>
<p id="atanh-example"></p>

<script>
function atanhExample() {
document.getElementById("atanh-example").innerHTML = Math.atanh(0.7);
}
</script>

</body>
</html>``````

Output:

Click the button to display the hyperbolic arctangent of 0.7

Note: If the parameter x is greater than 1, or less than -1, the method will return NaN. If the parameter x is 1, the method will return Infinity. If the parameter x is -1, the method will return -Infinity.

Enjoy coding!