JavaScript Math.tan() Method

Definition
Math.tan(x) returns the tangent of an angle. The angle must be in radians. Tangent is the sine divided by the cosine.
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.tan(x) | Returns the tangent of x, where x is in radians. |
Built into JavaScript. Call Math.tan(...) on the Math object — no import required. Math is not a constructor, so never write new Math().
Library declarations
Parameters
| Parameter | Description |
|---|
| x | Required. The angle in radians. |
Return Value
| Return Value | Description |
|---|
| Number | The tangent of x. Returns NaN if x is Infinity or not a number. |
JavaScript Examples
Example 1 — Basic Tangent Values
Try a few common angles written with Math.PI.
console.log(Math.tan(0)); // 0
console.log(Math.tan(Math.PI / 4)); // 0.9999999999999999
console.log(Math.tan(Math.PI / 4).toFixed(2)); // 1.00
0
0.9999999999999999
1.00
How It Works
- Zero.
Math.tan(0) is 0. - 45 degrees.
Math.PI / 4 is 45 degrees. The tangent should be 1. - Tiny error. The raw answer is
0.9999999999999999 because Math.PI is rounded. - Clean it up.
toFixed(2) shows "1.00".
Example 2 — Very Large Values Near 90 Degrees
At exactly 90 degrees the tangent has no value, so numbers get huge close to it.
console.log(Math.tan(Math.PI / 2)); // 16331239353195370
console.log(Math.tan(Math.PI / 2) > 1e15); // true
console.log(Math.tan(Infinity)); // NaN
16331239353195370
true
NaN
How It Works
- Almost 90 degrees.
Math.PI / 2 is a little less than the true 90 degrees. - Huge number. The result is about
1.6e16, not Infinity. - Comparison. The result is greater than
1e15, so we see true. - Infinity.
Math.tan(Infinity) is NaN.
Example 3 — Find the Height of a Tree
Stand 20 meters from a tree and look up at 30 degrees. Tangent gives the height.
const distance = 20;
const angleDeg = 30;
const height = distance * Math.tan(angleDeg * Math.PI / 180);
console.log(height.toFixed(2)); // 11.55
console.log(Math.tan("abc")); // NaN
How It Works
- Convert the angle.
30 * Math.PI / 180 changes 30 degrees into radians. - Use tangent. Height = distance × tan(angle).
- Result. The tree is about
11.55 meters tall. - Bad input.
Math.tan("abc") is NaN.
Compatibility
Browser & Runtime Support
Math.tan is part of the first JavaScript standard (ES1) and is Baseline Widely available in every browser and in Node.js.
✓ Baseline · ES1
Math.tan
Supported in Chrome, Firefox, Safari, Edge, Internet Explorer and Node.js for many years. No polyfill needed.
99% 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 supportMath.tan Excellent
Bottom line: Use Math.tan anywhere. It works in all browsers.
Frequently Asked Questions
Math.tan(x) returns the tangent of an angle x. The angle must be in radians.
Radians. To use degrees, convert with degrees * Math.PI / 180.
Math.PI is a rounded number, so Math.PI / 2 is slightly less than a true 90 degrees. The result is a very large number, about 1.6e16.
Because of floating point rounding, it returns 0.9999999999999999. Round with toFixed() if you want 1.00.
Math.atan() is the inverse. It takes a tangent value and returns the angle in radians.
All of them. It is part of the first JavaScript standard and works in every browser and in Node.js.
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