How to Use the Math.acos() Method in JavaScript

09/30/2021

Contents

In this article, you will learn how to use the Math.acos() method in JavaScript.

Using the Math.acos() method in JavaScript

The Math.acos() method is a built-in function in JavaScript that returns the arccosine (in radians) of a given number. The input parameter must be a value between -1 and 1. If the parameter is outside of this range, the method will return NaN (not a number).

Syntax

The syntax for using the Math.acos() method is:

Math.acos(x)

where x is the input parameter that represents the cosine of an angle.

Examples

Using Math.acos() to calculate the arccosine of a number

const x = 0.5;
const arccosine = Math.acos(x);
console.log(arccosine); // 1.0471975511965979

In this example, the input parameter is 0.5, which represents the cosine of an angle. The output is the arccosine of 0.5 in radians, which is approximately 1.0472.

Using Math.acos() with a value outside the range of -1 to 1

const x = 2;
const arccosine = Math.acos(x);
console.log(arccosine); // NaN

In this example, the input parameter is 2, which is outside the range of -1 to 1. Therefore, the output is NaN.

Using Math.acos() with a negative value

const x = -0.7;
const arccosine = Math.acos(x);
console.log(arccosine); // 2.418858405776377

In this example, the input parameter is -0.7, which represents the cosine of an angle. The output is the arccosine of -0.7 in radians, which is approximately 2.4189.

Note: The output of the Math.acos() method is always in radians. If you want to convert the result to degrees, you can use the following formula:

degrees = radians * (180 / Math.PI)

where radians is the output of the Math.acos() method.