In JavaScript, Math.acos() is a method of the Math object that returns the arccosine (inverse cosine) of a number.
The result is expressed in radians, which is a measure of angle.
Syntax
Math.acos(x)
x: A number between -1 and 1 (inclusive). The function calculates the arccosine of this number.
Returns
- The method returns a number between
0andπradians (0 to π, inclusive), which is the angle whose cosine isx.
Key Points
- Domain: The input value (
x) must be between-1and1, inclusive. Values outside this range result inNaN(Not-a-Number). - Range: The output is between
0andπradians.
Examples
Here are 10 examples demonstrating how Math.acos() works:
Basic Arccosine
console.log(Math.acos(1)); // 0 (cos(0) = 1)Arccosine of 0
console.log(Math.acos(0)); // 1.5707963267948966 (π/2 radians)Arccosine of -1
console.log(Math.acos(-1)); // 3.141592653589793 (π radians)Arccosine of 0.5
console.log(Math.acos(0.5)); // 1.0471975511965979 (π/3 radians)Arccosine of -0.5
console.log(Math.acos(-0.5)); // 2.0943951023931957 (2π/3 radians)Arccosine of 0.25
console.log(Math.acos(0.25)); // 1.318116071652818Arccosine of -0.25
console.log(Math.acos(-0.25)); // 1.8234765819369757Arccosine of a number slightly less than -1
console.log(Math.acos(-1.00001)); // NaN (Out of domain)Arccosine of a number slightly more than 1
console.log(Math.acos(1.00001)); // NaN (Out of domain)Arccosine of 0.9
console.log(Math.acos(0.9)); // 0.45102681179626236
Usage Notes
The result of
Math.acos()is in radians. To convert the result to degrees, you can use the formula:function radiansToDegrees(radians) { return radians * (180 / Math.PI); } let radians = Math.acos(0.5); console.log(radiansToDegrees(radians)); // 60 (degrees)Ensure that the input to
Math.acos()is within the valid range [-1, 1]. Values outside this range will returnNaN.