Categories
JavaScript Web development

JavaScript Math.acos()

JavaScript Math.acos()

The JavaScript Math.acos() returns the arccosine of a number as a value between 0 and PI radians.

Syntax:

Math.acos(x);

x – a number.

Example:

<!DOCTYPE html>
<html>
<body>

<button onclick="acosExample()">Try it</button>

<p id="example-1"></p>

<script>
function acosExample() {
  document.getElementById("example-1").innerHTML = Math.acos(0.8);
}
</script>

</body>
</html>

Output:

Click the button to display the arccosine of 0.8:


-1 will return the value of PI:

<!DOCTYPE html>
<html>
<body>


<button onclick="acosExample2()">Try it</button>

<p id="example-2"></p>

<script>
function acosExample2() {
  document.getElementById("example-2").innerHTML = Math.acos(-1);
}
</script>

</body>
</html>

Output:


If the parameter x is outside the range -1 to 1, the method will return NaN:

<!DOCTYPE html>
<html>
<body>


<button onclick="acosExample3()">Try it</button>

<p id="example-3"></p>

<script>
function acosExample3() {
  document.getElementById("example-3").innerHTML = Math.acos(1.2);
}
</script>

</body>
</html>

Output:


Enjoy coding!