JavaScript Math.atanh()
Syntax & Examples
Math.atanh() static-method
The Math.atanh() method in JavaScript returns the hyperbolic arctangent of a number, which must be between -1 and 1 (excluding -1 and 1). It is a static method of Math, meaning it is always used as Math.atanh() and not as a method of a Math object created (Math is not a constructor).
Syntax of Math.atanh()
The syntax of Math.atanh() static-method is:
Math.atanh(x)
This atanh() static-method of Math returns the hyperbolic arctangent of x.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
x | required | A number between -1 and 1 (excluding -1 and 1) for which to return the hyperbolic arctangent. |
Return Type
Math.atanh() returns value of type Number
.
✐ Examples
1 Using Math.atanh() with a positive number
In this example, we use the Math.atanh()
method to find the hyperbolic arctangent of 0.5.
For example,
- We call
Math.atanh()
with 0.5 as the argument. - The method returns the hyperbolic arctangent of 0.5.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.atanh(0.5));
Output
0.5493061443340548
2 Using Math.atanh() with a negative number
In this example, we use the Math.atanh()
method to find the hyperbolic arctangent of -0.5.
For example,
- We call
Math.atanh()
with -0.5 as the argument. - The method returns the hyperbolic arctangent of -0.5.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.atanh(-0.5));
Output
-0.5493061443340548
3 Handling invalid input with Math.atanh()
In this example, we demonstrate what happens when an invalid input (a number outside the range -1 to 1) is passed to Math.atanh()
.
For example,
- We call
Math.atanh()
with 2 as the argument. - The method returns NaN (Not-a-Number) because the input is outside the valid range.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.atanh(2));
Output
NaN
Summary
In this JavaScript tutorial, we learned about atanh() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.