JavaScript Math.acos()

Posted by

Method

JavaScript Math.acos() Method

The Math.acos() method in JavaScript is used to return the arccosine (in radians) of a given number. The arccosine of a number is the angle between 0 and π (pi) radians, or in other words, it is the inverse of the cosine function.

The syntax for this method is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Math.acos(x)

[/dm_code_snippet]

Where x is a number between -1 and 1, inclusive.

Let’s take a look at an example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Math.acos(0.5); // returns 1.04719755119660

[/dm_code_snippet]

This code returns the arccosine of 0.5 in radians, which is 1.04719755119660. If we convert this to degrees, we get 59.99999999999999 degrees.

Note: The Math.acos() method will return NaN if the value of x is greater than 1 or less than -1.

Browser Support

The Math.acos() method is supported in all major browsers, including Chrome, Firefox, Safari, Opera, and Internet Explorer.