The tanh() method calculates the hyperbolic tangent of the specified number and returns it.
Example
tanh() Syntax
The syntax of the Math.tanh() method is:
Math.tanh(number)
Here, tanh() is a static method. Hence, we are accessing the method using the class name, Math.
tanh() Parameters
The tanh() method takes a single parameter:
number- whose hyperbolic tangent is to be calculated
tanh() Return Value
The tanh() method returns:
- hyperbolic tangent of the given argument
number - NaN (Not a Number) for a non-numeric argument
Example 1: JavaScript Math.tanh()
In the above example, the Math.tanh() method computes the hyperbolic tangent of
-1(negative number) - results in -0.76159415595576490(zero) - results in 02(positive number) - results in 0.9640275800758169
Note: Mathematically, the hyperbolic tangent is equal to (ex - e-x)/(ex + e-x).
Example 2: Math.tanh() with Infinity Values
Here, the tanh() method, when used with infinities, returns -1 (if the parameter is -Infinity) and 1 (if the parameter is Infinity).
Example 3: Math.tanh() with Non-Numeric Argument
In the above example, we have tried to calculate the hyperbolic tangent of the string "Harry". That's why we get NaN as the output.
Recommended readings: