JavaScript Math acos()

The JavaScript Math.acos() function returns the arccosine of a number in radians.

Syntax of the Math.acos() function is:

Math.acos(x)

acos(), being a static method, is called using the Math class name.


Math.acos() Parameters

The Math.acos() function takes in:

  • x - A number whose arccosine is required.

Return value from Math.acos()

  • For values of x between -1 and 1, it returns the arccosine of a number between 0 and π radians.
  • For other values, it returns NaN.

Example: Using Math.acos()

// using Math.acos()

// Returns arccosine for -1 <= x <=1
var num = Math.acos(1);
console.log(num); // 0

var num = Math.acos(0.5);
console.log(num); // 1.0471975511965979 (pi/3)

var num = Math.acos(-1);
console.log(num); // 3.141592653589793 (pi)

// Returns NaN for x < -1 or x > 1
var num = Math.acos(100);
console.log(num); // NaN

Output

0
1.0471975511965979
3.141592653589793
NaN

Recommended readings: