How to get the tangent of a number in JavaScript?


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

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements