AppDividend
Latest Code Tutorials

JavaScript atan() Function: Complete Guide

The atan() method comes in handy in programming contexts dealing with trigonometric expressions. Moreover, since atan() is a static method of Math, it can be used without creating an object.

JavaScript Math atan()

Javascript Math atan() is a built-in function used to return the arctangent of a number in radians. The Math.atan() method returns the numeric value between -pi/2 and pi/2 radians. To find the arctangent value of a given argument in JavaScript, use the Math.atan() method. 

Javascript atan() is the static method of Math; therefore, it is always used as a Math.atan(), rather than as a method of the Math object created.

Syntax

Math.atan(x)

Parameter(s)

The variable x, whose arctangent value is to be determined.

Return value

The arctangent value between -pi/2 and pi/2 radians.

See the following figure.

JavaScript Math atan()

Note:

  • If the passed value is empty, the method returns NaN.

Compatibility

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari
  • Android webview
  • Chrome for Android
  • Firefox for Android
  • Opera for Android
  • Safari on iOS
  • Samsung Internet
  • Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

example1.js

The following code demonstrates the use of the Javascript atan() method.

// example1.js

var a = 1;
var b = 0;
var c = -0;
var d = Number.POSITIVE_INFINITY;
var e = Number.NEGATIVE_INFINITY;

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

Output

node example1
0.7853981633974483
0
-0
1.5707963267948966
-1.5707963267948966

Pass empty value to the atan() function(example2.js)

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

//example2.js

var a;
console.log(Math.atan(a));
console.log(Math.atan());

Output

node example2
NaN
NaN

example3.js

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

Given the three sides of a right-angled triangle, find all of its angles.

// example3.js

var h;
var b;
var p;

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

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

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

const prompt3 = () => {
  return new Promise((resolve, reject) => {
    rl.question('Perpendicular: ', (answer) => {
      p = answer;
      resolve();
    });
  });
};

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

  console.log('The three angles of the triangle are(in radians):');
  console.log(Math.atan(Number.POSITIVE_INFINITY)); 		//pi/2 radians
  console.log(Math.atan(p / b)); 					//base angle
  console.log(Math.atan(Number.POSITIVE_INFINITY) - Math.atan(p / b)); //third angle
}

main();

Output

Test Case 1:
->node example3
Hypotenuse: 6.928
Base: 6
Perpendicular: 3.464
The three angles of the triangle are:
1.5707963267948966
0.523586073612931
1.0472102531819656

Test Case2:
->node example3
Hypotenuse: 42.426
Base: 30
Perpendicular: 30
The three angles of the triangle are(in radians):
1.5707963267948966
0.7853981633974483
0.7853981633974483

Conclusion

The Javascript Math.atan() method returns an arctangent in radians of the number. The Math.atan() method returns the numeric value between -pi/2 and pi/2 radians.

See also

JavaScript Math atanh()

JavaScript Math atan2()

Javascript math cos()

Javascript math.cosh()

Javascript Math.abs()

Javascript Math.round()

Javascript Math.random()

Leave A Reply

Your email address will not be published.

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