JavaScript Math.acos() Method
-
Syntax of JavaScript
Math.acos()
Method -
Example 1: Use the
Math.acos()
Method With Values Between -1 to 1 -
Example 2: Use the
Math.acos()
Method With Values Not Between -1 to 1 -
Example 3: Use the
Math.acos()
Method With Non-Numeric Values
Programmers can use the Math.acos()
method to get the inverse cosine of the number value.
When we take a cosine of an angle, it gives the number values as an output, and an arc-cosine takes a number value and gives the angle in radians in the output.
Syntax of JavaScript Math.acos()
Method
let val = 0.5;
Math.acos(val);
Parameters
val
- The val
is a number in the range [-1,1]
.
Return
The Math.acos()
returns the angle related to val
in radians, means between [0,PI]
.
Example 1: Use the Math.acos()
Method With Values Between -1 to 1
In the example below, we have used the Math.acos()
method to get the inverse cosine value of different numbers in the range [-1,1]
. In the output, users can observe that it returns a single value, an angle in radians.
let val1 = 0.5;
let val2 = -0.5;
console.log(Math.acos(val1));
console.log(Math.acos(val2));
console.log(Math.acos(-0.0001));
console.log(Math.acos(0.999));
Output:
1.0471975511965979
2.0943951023931957
1.5708963267950633
0.044725087168733454
Example 2: Use the Math.acos()
Method With Values Not Between -1 to 1
When we take the val
parameter outside the [-1,-1]
, it always returns the NaN
values. The reason to return the NaN
value is the domain of the arc-cosine is [-1,-1]
, and when users try to find the output for the value that is out of the domain, it gives NaN
as an output.
let val1 = 1.5;
let val2 = -1.5;
console.log(Math.acos(val1));
console.log(Math.acos(val2));
Output:
NaN
NaN
Example 3: Use the Math.acos()
Method With Non-Numeric Values
In the example below, we are trying to find the output of the Math.acos()
method for the non-numeric values such as Infinity
and string values. In such a case, it returns the NaN
values as an output.
let val1 = Infinity;
let val2 = "Delft";
console.log(Math.acos(val1));
console.log(Math.acos(val2));
Output:
NaN
NaN
This article has seen various examples and use cases of the Math.acos()
method.