JavaScript Math tanh()

The tanh() method calculates the hyperbolic tangent of the specified number and returns it.

Example

// hyperbolic tangent of 1
let number = Math.tanh(1);
console.log(number); 

// Output: 0.7615941559557649

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()

// hyperbolic tangent of negative number let number1 = Math.tanh(-1)
; console.log(number1);
// hyperbolic tangent of zero let number2 = Math.tanh(0);
console.log(number2);
// hyperbolic tangent of positive number let number3 = Math.tanh(2);
console.log(number3); // Output: // -0.7615941559557649 // 0 // 0.9640275800758169

In the above example, the Math.tanh() method computes the hyperbolic tangent of

  • -1 (negative number) - results in -0.7615941559557649
  • 0 (zero) - results in 0
  • 2 (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

// tanh() with negative infinity let number1 = Math.tanh(-Infinity);
console.log(number1); // Output: -1
// tanh() with positive infinity let number2 = Math.tanh(Infinity);
console.log(number2); // Output: 1

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

let string = "Harry";

// tanh() with string argument let value = Math.tanh(string);
console.log(value); // Output: NaN

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.


Also Read:

Did you find this article helpful?