# How to get the arccosine (in radians) of a number in JavaScript?

Cosine is the ratio of the adjacent side and the hypotenuse of the right-angled triangle which is defined by them.

To get the cosine of a number, the Math.cos() function is used in JavaScript. The value returned will be between -1 and 1. This denotes the cosine of an angle. This will be in radians.

The inverse of cosine is called Arc cosine. An angle's trigonometric Arc cosine value can be calculated using acos() method. This returns a value between 0.0 and pi.

The input to acos() is one argument 1 ≥ n ≥ -1. And the function returns arc cosine value in radians. Mathematics representation is as follows. acos(n) = cos-1(n)=1/cos(n).

Next, we will discuss the method to find arccosine in JavaScript.

## Using the Math.acos() Method

Here, we will learn about the Math.acos() method in JavaScript. Math.acos() returns the arc cosine value of a number in JavaScript. The value returned is between 0 and π if the number is between -1 and 1. If the number is outside this range, this method returns NaN. This is an ECMAScript1(ES1) feature.

Google Chrome 1 and above, internet explorer 3 and above, Firefox 1 and above, Opera 3 and above, and Safari 1 and above are the supported browsers.

### Syntax

Math.acos( number );


Here, the number value must be between 1 and -1.

### Parameters

• number − This is the number for which the arccosine value in radians has to be found.

### Example 1

In this program, two p tags are created to display the output. Values between 1 and -1 are assigned to some variables for getting the arccosine value correctly. Values such as a string and value out of the range between 1 and -1 are assigned to another set of variables. Math.acos() executes all the inputs, and the output is displayed.

<html>
<body>
<h4> Finding the arccosine of a number in JavaScript using <i>Math.acos()</i></h4>
<p id = "acosDisp1"></p>
<p id = "acosDisp2"></p>
<script>
let a1 = Math.acos(0.75);
let a2 = Math.acos(-0.9);
let a3 = Math.acos(-1);
let a4 = Math.acos(0);
let a5 = Math.acos(0.5);
let a6 = Math.acos(1);
let a7 = Math.acos(Math.PI/10);
let b1 = Math.acos(-2);
let b2 = Math.acos(2);
let b3 = Math.acos("Learn");
let b4 = Math.acos(Math.PI/3);
var acosDispEl1 = document.getElementById("acosDisp1");
acosDispEl1.innerHTML = "Math.acos(0.75) = " + a1 + "<br>" + "Math.acos(-0.9) = " + a2 + "<br>" + "Math.acos(-1) = " + a3 + "<br>" +"Math.acos(0) = " + a4 + "<br>" + "Math.acos(0.5) = " + a5 + "<br>" +"Math.acos(1) = " + a6 + "<br>" + "Math.acos(Math.PI/10) = " + a7 +"<br>";
var acosDispEl2 = document.getElementById("acosDisp2");
acosDispEl2.innerHTML = "Math.acos(-2) = " + b1 + "<br>" + "Math.acos(2)= " + b2 + "<br>" + "Math.acos('Learn') = " + b3 + "<br>" + "Math.acos(Math.PI/3) = " + b4 + "<br>"
</script>
</body>
</html>

### Example 2

This example creates a single p tag to display the output. Here, input is given to Math.acos() like 1+1, 2-1, 0.1+0.1. This is the same as a mathematical operation. Based on the range of the result, the arccosine value is returned. That is 1+1=2 out of 1 and -1, which returns NaN. 2-1=1 which is within the range and the output is returned 0.

<html>
<body>
<h4> Finding the arccosine value of a number in JavaScript using <i>
Math.acos() </i> Math operation example </h4>
<p id = "acosNumOut"> </p>
<script>
var acosNumOutEl = document.getElementById("acosNumOut");
acosNumOutEl.innerHTML = "Math.acos(1 + 2) = " + Math.acos(1 + 2) + "<br>" + "Math.acos(0.1 + 0.2) = " + Math.acos(0.1 + 0.2) + "<br>" + "Math.acos(2 - 1)" + Math.acos(2 - 1);
</script>
</body>
</html>


This tutorial helped us to learn about Math.acos() in JavaScript. This method finds the arccosine of a number in radians. The method returns the value if the input is between 1 and -1. Else it returns NaN. We have also tried giving some operations along with the input, like 1+1. In such cases, the method also returns the respective output according to the input range.

Updated on: 31-Oct-2022

111 Views 