Javascript tanh() is a built-in static method of the Math library that accepts an argument and returns the hyperbolic tangent value.
To find the hyperbolic tangent value of a given argument in JavaScript, use the Math.tanh() method.
Syntax
Math.tanh(x)
Parameter(s)
The variable x, whose hyperbolic tangent value is to be determined.
Return Value
A number is the hyperbolic tangent value.
Polyfill
Polyfill is a code that can be used in place of this method for browsers that do not support the Javascript tanh() method.
Math.tanh = Math.tanh || function (x) { var a = Math.exp(x), b = Math.exp(-x); return a == Infinity ? 1 : b == Infinity ? -1 : (a - b) / (a + b); };
See the following figure.
Note
- If the passed value is not a valid number, the method returns NaN.
- If the passed value is ±Infinity, the method returns ±1.
- If the passed value is ±0, the method returns the value that is passed.
Compatibility(Version and above)
- Google Chrome v38
- Firefox v25
- Edge v12
- Opera v25
- Safari v8
- Android webview v38
- Chrome for Android v38
- Firefox for Android v25
- Opera for Android v25
- Safari on iOS v8
- Samsung Internet v3.0
- Node.js v0.12
Non-compatible with: Internet Explorer
JavaScript version: ECMAScript 6
Consider the following examples.
Javascript tanh() function example
See the following code.
// app.js let a = 1; let b = -1; let c = 10; let d = 4; let e = -2; let f = 2; console.log(Math.tanh(a)); console.log(Math.tanh(b)); console.log(Math.tanh(c)); console.log(Math.tanh(d)); console.log(Math.tanh(e)); console.log(Math.tanh(f));
Output
node example1 0.7615941559557649 -0.7615941559557649 0.9999999958776927 0.999329299739067 -0.9640275800758169 0.9640275800758169
Example 2
The following example demonstrates cases where values other than valid numbers are passed.
// app.js let a = "Hello, world"; let b; console.log(Math.tanh(a)); console.log(Math.tanh(b));
Output
node example2 NaN NaN
Example 3
The tanh() method cannot be used with complex arguments as only integer arguments are accepted.
// app.js // Complex values cannot be passed as arguments as follows // Since only integer arguments are accepted. console.log(Math.tanh(2 + i));
Output
ReferenceError: i is not defined
Example 4
The following example demonstrates the situation when an infinite value is passed.
// app.js let a = Infinity; let b = -Infinity; console.log(Math.tanh(a)); console.log(Math.tanh(b));
Output
node app 1 -1
Example 5
The following example demonstrates the situation when a positive or negative zero is passed.
// app.js let a = 0; let b = -0; console.log(Math.tanh(a)); console.log(Math.tanh(b));
Output
node example5 0 -0
Example 6
The following example demonstrates the use of polyfill for this method.
// app.js let a = 1; let b = -1; let c = 0; let d = 10; let e = Infinity; let f = -Infinity; function polyfill(x) { let a = Math.exp(x), b = Math.exp(-x); return a == Infinity ? 1 : b == Infinity ? -1 : (a - b) / (a + b); } console.log(Math.tanh(a)); console.log(Math.tanh(b)); console.log(Math.tanh(c)); console.log(Math.tanh(d)); console.log(Math.tanh(e)); console.log(Math.tanh(f)); console.log(); console.log(polyfill(a)); console.log(polyfill(b)); console.log(polyfill(c)); console.log(polyfill(d)); console.log(polyfill(e)); console.log(polyfill(f));
Output
node app 0.7615941559557649 -0.7615941559557649 0 0.9999999958776927 1 -1 0.7615941559557649 -0.7615941559557649 0 0.9999999958776926 1 -1
That’s it for this example.