Home JavaScript Math Methods tanh() JavaScript Math.tanh() 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.
Call What 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 Parameters Parameter Description x Required. Any number.
Return Value Return Value Description Number A number from -1 to 1. Returns NaN if x is not a number.
JavaScript Examples 1. Basic values 2. Limits 3. Squash numbers Example 1 — Basic Hyperbolic Tangent Values Try zero, one and minus one.
console.log(Math.tanh(0)); // 0
console.log(Math.tanh(1)); // 0.7615941559557649
console.log(Math.tanh(-1)); // -0.7615941559557649 0
0.7615941559557649
-0.7615941559557649 How It Works Zero. Math.tanh(0) is 0.Positive. Math.tanh(1) is about 0.7616.Negative. The same number with a minus sign.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.
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 0.9999999958776927
1
1
-1 How It Works Close to 1. Math.tanh(10) is just a tiny bit less than 1.Rounds to 1. For 100 the difference is too small to store, so it prints 1.Infinity. Math.tanh(Infinity) is 1.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.
const inputs = [-5, -1, 0, 1, 5];
inputs.forEach(function (x) {
console.log(x + " -> " + Math.tanh(x).toFixed(3));
}); -5 -> -1.000
-1 -> -0.762
0 -> 0.000
1 -> 0.762
5 -> 1.000 How It Works Many inputs. The array holds both small and large numbers.Squash. Math.tanh(x) maps each number into the range -1 to 1.Format. toFixed(3) shows three decimals.Pattern. Big numbers become close to 1.000 or -1.000. Numbers near zero stay small.Compatibility
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 What does Math.tanh() do? It returns the hyperbolic tangent of a number. The result is always from -1 to 1.
What is the difference between Math.tan() and Math.tanh()? Math.tan() works with angles in radians and can be very large. Math.tanh() works with any number and stays between -1 and 1.
What is Math.tanh(Infinity)? It is 1. Math.tanh(-Infinity) is -1.
Where is tanh used? It is often used in machine learning as an activation function, and anywhere you want to squash numbers into a small range.
What is the opposite of Math.tanh()? Math.atanh() is the inverse. It takes a number between -1 and 1 and returns the original input.
Which browsers support Math.tanh()? All modern browsers and Node.js. It was added in ES2015. Internet Explorer does not support it.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion