The Math.tan()
method is a built-in function in JavaScript that returns the tangent of a number. The tangent of an angle is the ratio of the length of the side opposite the angle to the length of the adjacent side. The tangent function via Math.tan()
is used in wide range of mathematical applications, including calculus, geometry, physics, and engineering. Some areas of application includes calculating slopes, periodic functions or derivatives.
In this article, we will explore the Math.tan()
method in detail, including syntax, examples, and usage.
Using the Math.tan()
method in JavaScript
Before we go into some examples of how to use the Math.tan()
method, let’s see the syntax of the Math.tan()
method which is as follows:
Math.tan(x)
The Math.tan()
method takes a single argument x
, which is the angle in radians. Now, that we have the syntax.
Example 1: Finding the Tangent of an Angle in Radians
In this example, we will find the tangent of an angle in radians using the Math.tan()
method. Let's find the tangent of 1.2
radians.
const angle = 1.2;
const tangent = Math.tan(angle);
console.log(tangent);
Output
2.5721516221263188
In the above example, we have passed 1.2
radians as an argument to the Math.tan()
method, which returns the tangent of the angle, which is 2.5721516221263277
.
Example 2: Finding the Tangent of an Angle Using Degrees
In this example, we will find the tangent of an angle in degrees using the Math.tan()
method. However, the Math.tan()
method expects the angle to be in radians. To convert degrees to radians, we can use the following formula:
radians = (Math.PI / 180) * degrees
Let's find the tangent of 30
degrees using the above formula.
const degrees = 30;
const radians = (Math.PI / 180) * degrees;
const tangent = Math.tan(radians);
console.log(tangent);
Output
0.5773502691896257
In the above example, we have first converted 30
degrees to radians using the formula (Math.PI / 180) * degrees
. Then we have passed the radians as an argument to the Math.tan()
method, which returns the tangent of the angle, which is 0.5773502691896257
.
Example 3: Finding the Tangent of Multiple Angles
In this example, we will find the tangent of multiple angles using the Math.tan()
method in a loop. Let's find the tangents of angles 0.25
, 0.5
, 0.75
, 1
, 1.25
, and 1.5
radians.
const angles = [0.25, 0.5, 0.75, 1, 1.25, 1.5];
angles.forEach((angle) => {
const tangent = Math.tan(angle);
console.log(`Tangent of ${angle} radians: ${tangent}`);
});
Output
Tangent of 0.25 radians: 0.25534192122103627
Tangent of 0.5 radians: 0.5463024898437905
Tangent of 0.75 radians: 0.9315964599440725
Tangent of 1 radians: 1.5574077246549023
Tangent of 1.25 radians: 3.0095696738628313
Tangent of 1.5 radians: 14.101419947171719
In the above example, we have created an array angles
containing the angles in radians. Then we have used a forEach
loop to iterate over the array and find the tangent of each angle using the Math.tan()
method. The method returns the tangent of each angle, which is printed to the console.
Summary
The Math.tan()
method is a powerful tool for performing mathematical calculations in JavaScript. In this article, we have explored some advanced examples of using the Math.tan()
method, including finding the tangent of an angle using degrees, finding the tangent of multiple angles, and creating a tangent chart. These examples demonstrate the versatility and usefulness of the Math.tan()
method in web development.
References
Math.tan() - JavaScript | MDN (mozilla.org)