AppDividend
Latest Code Tutorials

JavaScript Math tan() Function Example

0

Javascript Math tan() is an inbuilt function that finds the tangent value of a given argument, The Math.tan() method comes handy in Javascript contexts dealing with any trigonometric expressions. JS tan() is a static method of Math, and it can be used without creating an object.

JavaScript Math tan()

The Math.tan() method returns the numeric value that represents a tangent of the angle. Because tan() is the static method of Math, you always use it as Math.tan(), rather than as the method of the Math object you created ( Math is not a constructor).

Syntax

Math.tan(x)

Parameter(s)

The variable x (in radians) whose tangent value is to be determined.

Return Value

The tangent value between negative infinity and positive infinity.

 

JavaScript Math tan()

Note

  1. If the passed value is empty, this method returns NaN.
  2. For odd multiples of pi/2 radians passed as a parameter, this method returns a garbage value as the tan is not defined for these values.

Compatibility(Version and above)

  1. Google Chrome v1
  2. Internet Explorer v3
  3. Firefox v1
  4. Edge v12
  5. Opera v3
  6. Safari v1
  7. Android webview v1 
  8. Chrome for Android v18
  9. Firefox for Android v4
  10. Opera for Android v10.1
  11. Safari on iOS v1
  12. Samsung Internet v1.0
  13. Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

Javascript Math.tan()

The following code demonstrates the use of the Math.tan() method.

// app.js

let a = 0;
let b = 1;
let c = Math.PI;
let d = 2 * Math.PI;
let e = (Math.PI) / 4;

console.log(Math.tan(a));
console.log(Math.tan(b));
console.log(Math.tan(c));
console.log(Math.tan(d));
console.log(Math.tan(e));

Output

node app
0
1.5574077246549023
-1.2246467991473532e-16
-2.4492935982947064e-16
0.9999999999999999

Example 2

The following example demonstrates the case where an empty value is passed.

// app.js

let x;

console.log(Math.tan(x));
console.log(Math.tan());

Output

node app
NaN
NaN

Example 3

The following example demonstrates the case where odd multiples of pi/2 are passed.

// app.js

let a = (Math.PI) / 2;
let b = 3 * (Math.PI) / 2;
let c = 5 * (Math.PI) / 2;
let d = 7 * (Math.PI) / 2;

console.log(Math.tan(a));
console.log(Math.tan(b));
console.log(Math.tan(c));
console.log(Math.tan(d));

Output

node example3
16331239353195370
5443746451065123
3266247870639074
2333034193313624.5

Example 4

The following example demonstrates the application of this method in a simple programming context.

Given the base angle of a triangle and the base side, find all the remaining sides of the triangle.

// Given the base angle of a triangle and the base side,
// find all the remaining sides of the triangle.

let p;
let b;
let h;

let angle;

const r = require('readline');
const rl = r.createInterface({
  input: process.stdin,
  output: process.stdout
});

const prompt1 = () => {
  return new Promise((resolve, reject) => {
    rl.question('Base side: ', (answer) => {
      b = answer;
      resolve();
    });
  });
};

const prompt2 = () => {
  return new Promise((resolve, reject) => {
    rl.question('Base angle(in radians): ', (answer) => {
      angle = answer;
      resolve();
    });
  });
};

const main = async () => {
  await prompt1();
  await prompt2();
  rl.close();

  console.log('The three sides of the triangle are:');
  console.log(b); //base side

  let angle2 = (Math.PI) / 2 - angle;

  p = b * Math.tan(angle);
  console.log(p); //third side

  h = Math.sqrt(p * p + b * b);
  console.log(h); //hypotenuse
}

main();

Output

Test Case 1:
->node example4
Base side: 6
Base angle(in radians): 0.523599
The three sides of the triangle are:
6
3.464103410351597
6.928204127882605

Test Case2:
->node example4
Base side: 30
Base angle(in radians): 0.785398
The three sides of the triangle are:
30
29.99999019615471
42.42639993882793

See also

Javascript Math max()

Javascript Math exp()

Javascript Math pow()

Javascript Math tanh()

Javascript Math sinh()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.