The JavaScript Math tan() method is used to calculate the tangent of a number in radians. The tangent of an angle in a right-angled triangle is the ratio of the length of the opposite side to the length of the adjacent side.
Syntax
Math.tan(number)
Parameters
number(required): It is the angle in radians for which you want to calculate the tangent.
Return Value
Returns the tangent of a number.
- If the passed value is non-numeric or empty, this method returns NaN.
Visual Representation Example 1: How to Use Math tan() Method
let a = 2; //positive value
let b = -2; //negative value
let c = 1.3; //float value
let d = 0;
console.log(Math.tan(a));
console.log(Math.tan(b));
console.log(Math.tan(c));
console.log(Math.tan(d));
Output
-2.185039863261519
2.185039863261519
3.6021024479679786
0
Example 2: Handling non-numeric and empty value
console.log(Math.tan("JavaScript")); //non-numeric value
console.log(Math.tan()); //empty value
Output
NaN
NaN
Example 3: Passing PI as an argument
console.log(Math.tan(Math.PI));
console.log(Math.tan(Math.PI / 4)); //45 degrees
Output
-1.2246467991473532e-16
0.9999999999999999
Browser compatibility
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Opera 3 and above
- Safari 1 and above
Ankit Lathiya is a Master of Computer Application by education and Android and Laravel Developer by profession and one of the authors of this blog. He is also expert in JavaScript and Python development.