JavaScript Math acos()

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

Example


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

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

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

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


Recommended readings: