JavaScript Math.tan() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
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.

JavaScript
Math.tan(x)
CallWhat 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
JavaScript
Math.tan(x)

Parameters

ParameterDescription
xRequired. The angle in radians.

Return Value

Return ValueDescription
NumberThe 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.

JavaScript
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
Try It Yourself

How It Works

  1. Zero. Math.tan(0) is 0.
  2. 45 degrees. Math.PI / 4 is 45 degrees. The tangent should be 1.
  3. Tiny error. The raw answer is 0.9999999999999999 because Math.PI is rounded.
  4. 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.

JavaScript
console.log(Math.tan(Math.PI / 2)); // 16331239353195370
console.log(Math.tan(Math.PI / 2) > 1e15); // true
console.log(Math.tan(Infinity)); // NaN
Try It Yourself

How It Works

  1. Almost 90 degrees. Math.PI / 2 is a little less than the true 90 degrees.
  2. Huge number. The result is about 1.6e16, not Infinity.
  3. Comparison. The result is greater than 1e15, so we see true.
  4. 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.

JavaScript
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
Try It Yourself

How It Works

  1. Convert the angle. 30 * Math.PI / 180 changes 30 degrees into radians.
  2. Use tangent. Height = distance × tan(angle).
  3. Result. The tree is about 11.55 meters tall.
  4. Bad input. Math.tan("abc") is NaN.

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 support
Math.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.

Next: tanh()

Learn how to hyperbolic tangent.

tanh() 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