JavaScript Math.tanh() Method



The Math.tanh() method in JavaScript allows a numeric value as an argument and returns the hyperbolic tangent of that number. If we provide Infinity or -Infinity as an argument, this method returns 1 and -1, respectively. If a non-numeric or empty number is provided, it NaN as result.

Mathematically, the hyperbolic tangent function is defined as follows −

 tanh(x) = (e^x - e^(-x)) / (e^x + e^(-x)) 

Where x is the input number, and e is the base of the natural logarithm (approximately equal to 2.71828).

Note: This method is not supported in Internet Explorer.

Syntax

Following is the syntax of JavaScript Math.tanh() method −

 Math.tanh(x) 

Parameters

This method accepts only one paramter. The same is described below −

  • x: A numeric value.

Return value

This method returns the hyperbolic tangent of the provided number.

Example 1

Following example demonstartes the basic usage of JavaScript Math.tanh() method −

 <html> <body> <script> let number1 = Math.tanh(-1); document.write(number1, "<br>"); let number2 = Math.tanh(0); document.write(number2, "<br>"); let number3 = Math.tanh(1); document.write(number3); </script> </body> </html> 

Output

If we execute the above program, it returns the hyperbolic tangent for the provided numbers.

Example 2

Here, we are using the Math.tanh() method with Infinity values −

 <html> <body> <script> let number1 = Math.tanh(Infinity); document.write(number1, "<br>"); let number2 = Math.tanh(-Infinity); document.write(number2); </script> </body> </html> 

Output

If we execute the above program, it returns (1) for Infinity and (-1) for -Infinity as result.

Example 3

If we try to calculate the hyperbolic tangent value of a non-numeric value or empty number, it returns NaN as result −

 <html> <body> <script> let number1 = Math.tanh("Tutorialspoint"); document.write(number1, "<br>"); let number2 = Math.tanh(); document.write(number2); </script> </body> </html> 

Output

As we can see in the output, the above program returned NaN.

Advertisements