Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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:
tan ? = opposite side / adjacent side also, tan ? = sin ? / cos ? Where ? = angle in radians
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. If the parameter is empty or the variable is a non-numeric value, it returns NaN, which denotes "Not a Number."
Syntax
Math.tan(number)
Parameters
number ? A number in radians whose tangent needs to be calculated.
Return Value
number ? The tangent of the given number.
NaN ? If the argument is empty or a non-numeric value.
Example: Basic Usage with Radians
In the below example, we have used the Math.tan() method to find the tangent of numbers in radians.
<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 = 0; // 0 radians
let number2 = Math.PI/4; // ?/4 radians (45 degrees)
let number3 = Math.PI/3; // ?/3 radians (60 degrees)
let text = 'abcd';
let root = document.getElementById('root');
root.innerHTML = 'The tangent of ' + number1 + ' radians is: ' + Math.tan(number1) + '<br>';
root.innerHTML += 'The tangent of ?/4 radians is: ' + Math.tan(number2) + '<br>';
root.innerHTML += 'The tangent of ?/3 radians is: ' + Math.tan(number3) + '<br>';
root.innerHTML += 'The tangent of "' + text + '" is: ' + Math.tan(text) + '<br>';
</script>
</body>
</html>
The tangent of 0 radians is: 0 The tangent of ?/4 radians is: 1 The tangent of ?/3 radians is: 1.7320508075688772 The tangent of "abcd" is: NaN
Converting Degrees to Radians
The Math.tan() method takes a parameter in radians, but if you want to get the tangent of a number in degrees, you need to convert degrees to radians first using the formula: radians = degrees × ? / 180.
Syntax for Degrees
function degreesToRadians(degrees) {
return degrees * Math.PI / 180;
}
Math.tan(degreesToRadians(degrees))
Example: Using Degrees
In the below example, we find the tangent of numbers in degrees by converting them to radians first.
<html>
<body>
<h4>Get the tangent of a number (in degrees) using <i>Math.tan()</i> method in JavaScript</h4>
<div id="root"></div>
<script>
function tangentFromDegrees(degrees) {
return Math.tan((degrees * Math.PI) / 180);
}
let angle1 = 0; // 0 degrees
let angle2 = 30; // 30 degrees
let angle3 = 45; // 45 degrees
let angle4 = 60; // 60 degrees
let root = document.getElementById('root');
root.innerHTML = 'The tangent of ' + angle1 + '° is: ' + tangentFromDegrees(angle1) + '<br>';
root.innerHTML += 'The tangent of ' + angle2 + '° is: ' + tangentFromDegrees(angle2).toFixed(4) + '<br>';
root.innerHTML += 'The tangent of ' + angle3 + '° is: ' + tangentFromDegrees(angle3).toFixed(4) + '<br>';
root.innerHTML += 'The tangent of ' + angle4 + '° is: ' + tangentFromDegrees(angle4).toFixed(4) + '<br>';
</script>
</body>
</html>
The tangent of 0° is: 0 The tangent of 30° is: 0.5774 The tangent of 45° is: 1.0000 The tangent of 60° is: 1.7321
Common Tangent Values
| Angle (Degrees) | Angle (Radians) | Tangent Value |
|---|---|---|
| 0° | 0 | 0 |
| 30° | ?/6 | ? 0.5774 |
| 45° | ?/4 | 1 |
| 60° | ?/3 | ? 1.7321 |
Conclusion
Use Math.tan() to calculate the tangent of angles in radians. For degrees, multiply by Math.PI/180 first to convert to radians. The method returns precise tangent values for mathematical calculations.
