# How to get the tangent of a number in JavaScript?

JavascriptWeb DevelopmentFront End Technology

#### JavaScript for beginners

Best Seller

74 Lectures 10 hours

#### Modern Javascript for Beginners + Javascript Projects

Most Popular

112 Lectures 15 hours

#### The Complete Full-Stack JavaScript Course!

Best Seller

96 Lectures 24 hours

In this tutorial, we will learn how to get the tangent of a number in JavaScript. The tangent of an angle is defined by the ratio of the length of the opposite side to the length of the adjacent side. It can also be defined as the ratio of sine and cosine function of an acute angle where the value of cosine function should not be zero.

Mathematically,

𝑡𝑎𝑛𝜃=𝑙𝑒𝑛𝑔𝑡ℎ 𝑜𝑓 𝑡ℎ𝑒 𝑜𝑝𝑝𝑜𝑠𝑖𝑡𝑒 𝑠𝑖𝑑𝑒 / 𝑙𝑒𝑛𝑔𝑡ℎ 𝑜𝑓 𝑡ℎ𝑒 𝑎𝑑𝑗𝑎𝑐𝑒𝑛𝑡 𝑠𝑖𝑑𝑒
also, tanθ=(𝑠𝑖𝑛𝑒 𝑜𝑓 𝑡ℎ𝑒 𝑎𝑛𝑔𝑙𝑒 𝜃)⁄(𝑐𝑜𝑠𝑖𝑛𝑒 𝑜𝑓 𝑡ℎ𝑒 𝑎𝑛𝑔𝑙𝑒 𝜃)
Where 𝜃 = acute angle in radian


The tangent of a number in JavaScript can be found using Math.tan() method, which is a built-in method of JavaScript.

## Using the Math.tan() Method

In JavaScript, The Math.tan() method is useful for finding the tangent of a number. It takes a variable as a parameter that is expected to be a number in radians and returns the tangent value of that variable. Otherwise, if the parameter is empty or the variable is a non-numeric value, it returns NaN, which denotes “Not a Number.”

Users can follow the below syntax to find the tangent of a number using the Math.tan() method.

### Syntax

Using the following syntax, we can get the number of forms in an HTML document with a <form> tag.

let number = 20; // angle in radians
let tangent = Math.tan(number) // 2.237160944224742


### Parameters

• number− it takes a variable that is expected to be a number in radians whose tangent needs to be found.

### Return Type

• tangent− of the given number.

• NaN− if the argument is empty or a non−numeric value.

### Example

In the below example, we have used the Math.tan() method to find the tangent of a number. We have taken the different values to observe the output of the Math.tan() method.

<html>
<body>
<h4>Get the tangent of a number using <i>Math.tan()</i> method in JavaScript</h4>
<div id="root"></div>
<script>
let number1 = 20
let number2 = 50
let number3 = 180
let text = 'abcd'
let root = document.getElementById('root')
root.innerHTML ='The tangent of ' + number1 + ' is: ' + Math.tan(number1) + '<br>'
root.innerHTML +='The tangent of ' + number2 + ' is: ' + Math.tan(number2) + '<br>'
root.innerHTML +='The tangent of ' + number3 + ' is: ' + Math.tan(number3) + '<br>'
root.innerHTML +='The tangent of ' + text + ' is: ' + Math.tan(text) + '<br>'
</script>
</body>
</html>


In the above output, users can see that Math.tan() method returns the desired tangent value for given numbers, and for non−numeric values, it returns NaN.

## Using the Math.tan() Method for Numbers in Degree

The Math.tan() method takes a parameter that should be a number in radians, but if you want to get the tangent of a number in degree, you need a few changes in the code.

### Syntax

function tangent(degree) {
return Math.tan((degree * Math.PI) / 180)
}


In the above code, we are taking the degree as a parameter which is expected to be a number in degree, and then convert the degree into the radians by using (Math.PI/180), which is the formula for converting degree into radian and then using the Math.tan() method we are getting the tangent value of that given number.

### Algorithm

Step 1 − Declare a function that takes an argument that is expected to be a number in degree

Step 2 − Convert the number in radians by (Math.PI/180) inside the Math.tan() method

Step 3 − Return the value of the Math.tan() method

### Example

In the below example, we find the tangent of a number in degree. We have taken the different values to observe the output.

<!DOCTYPE html>
<html>
<body>
<h4>Get the tangent of a number (in degree) using <i>Math.tan()</i> method in JavaScript</h4>
<div id="root"></div>
<script>
function tangent(degree) {
return Math.tan((degree * Math.PI) / 180)
}
let number1 = 30
let number2 = 45
let number3 = 60
let text = 'abcd'
let root = document.getElementById('root')
root.innerHTML ='The tangent of ' + number1 + ' degree is: ' + tangent(number1) + '<br>'
root.innerHTML +='The tangent of ' + number2 + ' degree is: ' + tangent(number2) + '<br>'
root.innerHTML +='The tangent of ' + number3 + ' degree is: ' + tangent(number3) + '<br>'
root.innerHTML +='The tangent of ' + text + ' is: ' + tangent(text) + '<br>'
</script>
</body>
</html>


In the above output, users can see that we take numbers that are in degree, and by using the tangent function, we are getting the value of the tangent. The tangent function is first converted the number into radians, and then using Math.tan() method, it returns the desired tangent value for that given number. For non−numeric values, it returns NaN.

We have learned how to get the tangent value of a number using JavaScript. We learned two ways of getting the tangent value. The first approach is used for getting the tangent value if the number is in radians. The second approach is used for getting the tangent value if the number is in degree. It is recommended to use the Math.tan() method for getting the tangent value of a number as it is a built−in method of JavaScript.

Updated on 14-Sep-2022 08:50:08