JavaScript Math.acos() Method



The JavaScript Math.acos() method accepts a number as a parameter and calculates the arccosine (inverse cosine) of a number. The result is the angle (in radians) whose cosine is the specified number.

If the argument provided to this method is in between -1 and 1 (inclusive), it returns the angle in radians between 0 and PI(). If the argument is outside the range of -1 or 1 or non-numeric, it returns "NaN" as result.

Syntax

Following is the syntax of JavaScript Math.acos() method −

Math.acos(x);

Parameters

This method accepts only one parameter. The same is described below −

  • x: A numeric value between -1 and 1, representing the cosine of an angle.

Return Value

This method returns the arccosine (inverse cosine) of the provided number in radians.

Example 1

In the following example, we are using the JavaScript Math.acos() method with arguments between -1 and 1 −

<html>
<body>
<script>
   let number1 = Math.acos(-1);
   document.write(number1, "<br>");

   let number2 = Math.acos(0);
   document.write(number2, "<br>");

   let number3 = Math.acos(0.5);
   document.write(number3, "<br>");

   let number4 = Math.acos(1);
   document.write(number4);
</script>
</body>
</html>

Output

It returns the arccosine of the provided numbers.

Example 2

In this example, we are using the Math.acos() method with arguments outside the range of -1 to 1 −

<html>
<body>
<script>
   let number1 = Math.acos(-2);
   document.write(number1, "<br>");

   let number2 = Math.acos(2);
   document.write(number2);
</script>
</body>
</html>

Output

If we execute the above program, it returns "NaN" as result.

Example 3

Here, we are passing an argument which is not a numeric value −

<html>
<body>
<script>
   let string = "Tutorialspoint";
   let number = Math.acos("Tutorialspoint");

   document.write(number);
</script>
</body>
</html>

Output

If we execute the above program, it returns "NaN" as result.