JavaScript Math.tanh() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

Math.tanh(x) returns the hyperbolic tangent of a number. The result is always between -1 and 1, so it is useful for squashing any number into a small range.

Read the syntax below, then try the three examples to see the method in action.

Syntax

Call the method on Math. Start with this line.

JavaScript
Math.tanh(x)
CallWhat it does
Math.tanh(x)Returns the hyperbolic tangent of x.

Built into JavaScript. Call Math.tanh(...) on the Math object — no import required. Math is not a constructor, so never write new Math().

Library declarations
JavaScript
Math.tanh(x)

Parameters

ParameterDescription
xRequired. Any number.

Return Value

Return ValueDescription
NumberA number from -1 to 1. Returns NaN if x is not a number.

JavaScript Examples

Example 1 — Basic Hyperbolic Tangent Values

Try zero, one and minus one.

JavaScript
console.log(Math.tanh(0)); // 0
console.log(Math.tanh(1)); // 0.7615941559557649
console.log(Math.tanh(-1)); // -0.7615941559557649
Try It Yourself

How It Works

  1. Zero. Math.tanh(0) is 0.
  2. Positive. Math.tanh(1) is about 0.7616.
  3. Negative. The same number with a minus sign.
  4. Small range. None of the results leave -1 to 1.

Example 2 — The Result Stays Between -1 and 1

As the input gets bigger, the result gets closer to 1 but never goes beyond it.

JavaScript
console.log(Math.tanh(10)); // 0.9999999958776927
console.log(Math.tanh(100)); // 1
console.log(Math.tanh(Infinity)); // 1
console.log(Math.tanh(-Infinity)); // -1
Try It Yourself

How It Works

  1. Close to 1. Math.tanh(10) is just a tiny bit less than 1.
  2. Rounds to 1. For 100 the difference is too small to store, so it prints 1.
  3. Infinity. Math.tanh(Infinity) is 1.
  4. Negative side. Negative infinity gives -1.

Example 3 — Squash Numbers into a Small Range

Loop through some inputs and print the squashed value. This idea is used in simple machine learning code.

JavaScript
const inputs = [-5, -1, 0, 1, 5];
inputs.forEach(function (x) {
  console.log(x + " -> " + Math.tanh(x).toFixed(3));
});
Try It Yourself

How It Works

  1. Many inputs. The array holds both small and large numbers.
  2. Squash. Math.tanh(x) maps each number into the range -1 to 1.
  3. Format. toFixed(3) shows three decimals.
  4. Pattern. Big numbers become close to 1.000 or -1.000. Numbers near zero stay small.

Browser & Runtime Support

Math.tanh was added in ES2015 and is Baseline Widely available in modern browsers and Node.js. It does not work in Internet Explorer.

✓ Baseline · ES2015

Math.tanh

Supported in Chrome, Firefox, Safari, Edge and Node.js for years. Only very old browsers such as Internet Explorer lack it.

98% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Math.tanh Excellent

Bottom line: Use Math.tanh in modern code. Add a small polyfill only if you must support Internet Explorer.

Frequently Asked Questions

It returns the hyperbolic tangent of a number. The result is always from -1 to 1.
Math.tan() works with angles in radians and can be very large. Math.tanh() works with any number and stays between -1 and 1.
It is 1. Math.tanh(-Infinity) is -1.
It is often used in machine learning as an activation function, and anywhere you want to squash numbers into a small range.
Math.atanh() is the inverse. It takes a number between -1 and 1 and returns the original input.
All modern browsers and Node.js. It was added in ES2015. Internet Explorer does not support it.

Next: trunc()

Learn how to remove the decimal part.

trunc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful