AppDividend
Latest Code Tutorials

# JavaScript Math tanh() Function Example

If you want to find the hyperbolic tangent value of a given argument in JavaScript, then use the Math.tanh() method is used. Javascript tanh() method comes handy in programming contexts dealing with any trigonometric expressions. The tanh() is a static method of Math, and it can be used without creating an object.

## JavaScript Math tanh()

Javascript tanh() is a static method of Math, and it can be used without creating an object.

### Syntax

```Math.tanh(x)
```

### Parameter(s)

The variable x, whose hyperbolic tangent value is to be determined.

### Return Value

A number which 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

1. If the passed value is not a valid number, the method returns NaN.
2. If the passed value is ±Infinity, the method returns ±1.
3. If the passed value is ±0, the method returns the value that is passed.

### Compatibility(Version and above)

2. Firefox v25
3. Edge v12
4. Opera v25
5. Safari v8
6. Android webview v38
7. Chrome for Android v38
8. Firefox for Android v25
9. Opera for Android v25
10. Safari on iOS v8
11. Samsung Internet v3.0
12. 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 the case 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```

Javascript Math max()

Javascript Math exp()

Javascript Math pow()

Javascript Math abs()

Javascript Math sqrt()

Javascript Math min()

This site uses Akismet to reduce spam. Learn how your comment data is processed.