JavaScript Math.tan() Method



The JavaScript Math.tan() method allows a numeric value as an arguemnt (representing the angle in radians) and calculates the trigonometric tangent the number. If the provided argument to this method is NaN or Infinity values, it returns NaN as result.

Syntax

Following is the syntax of JavaScript Math.tan() method −

Math.tan(x)

Parameters

This method accepts only one parameter. The same is described below −

  • x: A numeric value representing an angle in radians.

Return value

This method returns the tangent of the given number in radians.

Example 1

In the following example, we are using the JavaScript Math.tan() method to calculate the tangent of 2 radians−

<html>
<body>
<script>
   const result = Math.tan(4);
   document.write(result);
</script>
</body>
</html>

Output

If we execute the above program, it will return approximately "1.1578".

Example 2

Here, we are computing the tangent value of negative -4 radians −

<html>
<body>
<script>
   const result = Math.tan(-4);
   document.write(result);
</script>
</body>
</html>

Output

It will return approximately "-1.157" as result.

Example 3

In the below example, we are computing the tangent value of math constant "PI" −

<html>
<body>
<script>
   const result = Math.tan(Math.PI);
   document.write(result);
</script>
</body>
</html>

Output

The output -1.2246467991473532e-16 represents -1.2246467991473532 × 10-16

Example 4

If we try to calculate the tangent value of a "string", this method will return NaN as result −

<html>
<body>
<script>
   const result = Math.tan("Tutorialspoint");
   document.write(result);
</script>
</body>
</html>

Output

As we can see in the output below, it returned NaN as result.

Example 5

The Math.tan() method doesn't treat "Infinity" as a number, so if we pass this as an argument, this method returns NaN as result −

<html>
<body>
<script>
   const result1 = Math.tan(Infinity);
   const result2 = Math.tan(-Infinity);
   document.write(result1, "<br>", result2);
</script>
</body>
</html>

Output

It is because the tangent of an angle cannot be infinite.

Advertisements