The tan()
method computes the trigonometric tangent of the specified angle and returns it.
Example
// 1 represents angle in radian
let value = Math.tan(1);
console.log(value);
// Output: 1.5574077246549023
tan() Syntax
The syntax of the Math.tan()
method is:
Math.tan(angle)
Here, tan()
is a static method. Hence, we are accessing the method using the class name, Math
.
tan() Parameter
The tan()
method takes a single parameter:
angle
- in radians whose tangent value is to be calculated
tan() Return Value
The tan()
method returns:
- tangent of a given
angle
(in radians) - NaN (Not a Number) for a non-numeric argument
Example 1: JavaScript Math.tan()
// tangent of 5 radian
let value1 = Math.tan(5);
console.log(value1);
// negative radians are allowed
let value2 = Math.tan(-2);
console.log(value2);
// Output:
// -3.380515006246586
// 2.185039863261519
In the above example,
Math.tan(5)
- calculates the tangent of5
Math.tan(-2)
- calculates the tangent of-2
Example 2: Math.tan() with Math Constants
// math constants can be used
let value = Math.tan(Math.PI);
console.log(value);
// Output: -1.2246467991473532e-16
In the above example, we have used the tan()
method to compute the tangent of the math constant PI
.
Here, the output -1.2246467991473532e-16 represents -1.2246467991473532 * 10-16.
Example 3: Math.tan() with Non-Numeric argument
let string = "Luke";
// tan() with a string argument
let value = Math.tan(string);
console.log(value);
// Output: NaN
In the above example, we have used the tan()
method with a string argument.
When using a string argument, the method gives us NaN as output.
Example 4: Math.tan() with Infinity argument
// tan() with infinity
let value1 = Math.tan(Infinity);
console.log(value1);
// tan() with negative infinity
let value2 = Math.tan(-Infinity);
console.log(value2);
// Output:
// NaN
// NaN
The tan()
method doesn't treat -Infinity
and Infinity
as numbers and produces NaN as output.
This is because the tangent of an angle can never be infinite.
Recommended readings: