JavaScript Math acos()

The acos() method calculates the arc-cosine (inverse of cosine) of the specified angle and returns it.

Example

let value = Math.acos(0.45);
console.log(value); 

// Output: 1.1040309877476002

acos() Syntax

The syntax of the Math.acos() method is:

Math.acos(angle)

Here, acos() is a static method. Hence, we are accessing the method using the class name, Math.


acos() Parameter

The acos() method takes a single parameter:

  • angle (radians) - in range -1 to 1 whose arc-cosine is to be calculated

acos() Return Value

The acos() method returns:

  • arc-cosine value of the angle
  • NaN (Not a Number) for argument greater than 1 or less than -1 or non-numeric

Example 1: Math.acos() with Arguments Between -1 and 1

// arc-cosine of negative number let number1 = Math.acos(-1);
console.log(number1);
// arc-cosine of positive number let number2 = Math.acos(0.5);
console.log(number2); // Output: // 3.141592653589793 // 1.0471975511965979

In the above example, the Math.acos() method computes the arc-cosine of

  • -1 (negative number) - results in 3.141592653589793
  • 0.5 (positive number) - results in 1.0471975511965979

Example 2 : Math.acos() with Arguments Not Between -1 and 1

// argument less than -1 let number1 = Math.acos(-3);
console.log(number1); // Output: NaN
// argument greater than 1 let number2= Math.acos(32)
; console.log(number2); // Output: NaN

Here, we have used the Math.acos() method with values -3 (less than -1) and 32 (greater than 1). For both values, we get NaN as output.


Example 3: Math.acos() with Non-Numeric Argument

let string = "Harry";
// acos() with a string argument let value = Math.acos(string);
console.log(value); // Output: // NaN

In the above example, we have used Math.acos() with a string value, "Harry". Hence, we get NaN as the output.


Recommended readings: