The Math.tan()
method in JavaScript is used to compute the tangent of a given number. The tangent function is one of the most fundamental trigonometric functions, which calculates the ratio of the opposite side to the adjacent side of a right triangle. In the context of the unit circle, it is the ratio of the sine to the cosine of an angle.
Syntax
Math.tan(x);
Parameters
- x (Required): A number representing the angle in radians. This is the value for which the tangent will be calculated. The value of
x
can be any valid number, including positive or negative numbers, and can even be0
.
Return Value
The Math.tan()
method returns a number that represents the tangent of the input value. The output can be any real number, including:
- Positive values when the angle is in the range of 0 to π/2 (first quadrant).
- Negative values when the angle is in the range of π to 3π/2 (third quadrant).
- The function approaches infinity (∞) at angles where cos(x) is zero, such as π/2, 3π/2, etc.
Special Cases
- If the input is 0, the result will also be 0.
- For very large positive or negative input values, the tangent function’s result may approach a very large value or infinity.
Example 1: Basic example to use Math.tan() method.
let result = Math.tan(0); console.log(result); // Output: 0
Since the tangent of 0 radians is 0, the result is 0
.
Example 2: Using a positive angle in Math.tan() method.
let result = Math.tan(Math.PI / 4); console.log(result); // Output: 1
Example 3: Using a negative angle in Math.tan() method.
let result = Math.tan(-Math.PI / 4); console.log(result); // Output: -1
Example 4: Angle Leading to Infinity.
let result = Math.tan(Math.PI / 2); console.log(result); // Output: Infinity
Supported Browsers
Browser | Version Supported |
---|---|
Chrome | 1.0+ |
Firefox | 3.5+ |
Safari | 4.0+ |
Edge | 12+ |
Internet Explorer | 9+ |
Opera | 10.50+ |