In JavaScript, the Math object is used to perform mathematical
operations. It provides a variety of methods and properties for
performing common mathematical calculations. The Math object is built
into the language and can be used without the need for any additional
libraries or modules.
Using the Math.acos() method in JavaScript
The Math.acos() is a built-in JavaScript function used to calculate
the arccosine of a number. The arccosine is the inverse cosine function,
which is the angle whose cosine is a given number. The input to
Math.acos() must be a number between -1 and 1. If the input is outside
this range, the function will return NaN (not a number).
The syntax for the Math.acos() can be seen below
Math.acos(ang);
where the ang value is the angle in radians
The Math.acos() method can be useful in a variety of applications,
including computer graphics, physics, and engineering. Here are some
examples of how to use the Math.acos() method in JavaScript.
const angle = Math.acos(0.5);
console.log(angle);
Output
1.0471975511965979
In this example, we calculate the arccosine of 0.5 using the
Math.acos() method. The method returns the angle whose cosine is 0.5,
which is approximately 1.047 radians.
Example 1: Finding angle
Here we find the angle between two positions using the inverse cosine.
const x = 0.5;
const y = Math.sqrt(1 - x * x);
const angle = Math.acos(x);
console.log(`x: ${x}, y: ${y}, angle: ${angle}`);
Output
x: 0.5, y: 0.8660254037844386, angle: 1.0471975511965979
In this example, we use the Pythagorean theorem to calculate the value
of y, given x = 0.5. We then use the Math.acos() method to
calculate the arccosine of x. The method returns the angle whose
cosine is 0.5, which is approximately 1.047 radians.
Example 2: Finding the angle between two vectors
Suppose we have two vectors, v1 and v2, in two-dimensional space,
and we want to find the angle between them in degrees. We can use the
arccosine function to do this as follows:
const v1 = [2, 2];
const v2 = [7, 4];
const cosAngle =
(v1[0] * v2[0] + v1[1] * v2[1]) / (Math.hypot(...v1) * Math.hypot(...v2));
const angleDegrees = (Math.acos(cosAngle) * 180) / Math.PI;
console.log(angleDegrees);
Output
15.255118703057764
Here, we first calculate the dot product of the two vectors and divide
it by the product of their magnitudes to get the cosine of the angle
between them. We then use the Math.acos() function to find the
arccosine of this cosine value, which gives us the angle in radians.
Finally, we convert the angle to degrees by multiplying by 180 and
dividing by π.
Example 3: Solving a triangle
Suppose we have a triangle with sides a, b, and c, and we know the
lengths of a and b and the angle C opposite c. We can use the
inverse cosine function to find the length of c as follows:
const a = 3;
const b = 4;
const C = (30 * Math.PI) / 180;
const c = Math.sqrt(a * a + b * b - 2 * a * b * Math.cos(C));
console.log(c);
Output
2.0531415706603067
Here, we use the law of cosines to find the length of the third side
c, given the lengths of the other two sides and the angle opposite
c. The law of cosines states that c^2 = a^2 + b^2 - 2ab*cos(C),
where C is the angle opposite c. We use the Math.cos() function to
find the cosine of C, and then use the inverse cosine function to find
the angle in radians. We then use the Math.sqrt() function to find the
square root of the right-hand side of the equation, which gives us the
length of c.
Example 4: Generating random points on a circle
Suppose we want to generate n random points on the circumference of a
circle with radius r. We can use the inverse cosine function to find
the x and y coordinates of each point as follows:
const n = 10;
const r = 5;
for (let i = 0; i < n; i++) {
const theta = Math.random() * 2 * Math.PI;
const x = r * Math.cos(theta);
const y = r * Math.sin(theta);
console.log(`Point ${i}: (${x}, ${y})`);
}
Output
Point 0: (4.081395152354311, 2.888289045497131)
Point 1: (4.720956639499305, -1.646987676932474)
Point 2: (4.563814085003422, 2.0424497539779)
Point 3: (-3.2993302003682596, 3.7569163191290187)
Point 4: (0.19855841778121402, -4.996055899880247)
Point 5: (4.998591842862364, -0.11865744169598699)
Point 6: (4.732863031675021, -1.6124538825665466)
Point 7: (-4.731321838371784, 1.6169705197517492)
Point 8: (4.709397837440632, -1.6797535559448895)
Point 9: (-3.830151061490476, -3.2140228446859833)
Here, we use the Math.random() function to generate a random angle
between 0 and 2π radians. We then use the Math.cos() and Math.sin()
functions to calculate the x and y coordinates of each point on the
circle, given the radius r and the angle theta. We repeat this n
times to generate n random points on the circle.
Bonus Tips
Here are some additional important things to know about the
Math.acos() function in JavaScript:
The output of Math.acos() is always in radians, not degrees. If you
need to convert the result to degrees, you can use the toDegrees()
function like this:
function toDegrees(angle) {
return angle * (180 / Math.PI);
}
let x = 0.5;
let angle = Math.acos(x); // Returns the angle whose cosine is 0.5
console.log(toDegrees(angle)); // Outputs 60 (degrees)
The Math.acos() function is the inverse of the Math.cos() function.
That is, if you take the cosine of an angle and then take the arccosine
of that result, you should get back the original angle. However, due to
rounding errors, this may not always be the case.
let angle = 45; // degrees
let cosAngle = Math.cos(angle * Math.PI / 180); // Takes the cosine of 45 degrees
console.log(cosAngle); // Outputs 0.7071067811865476
let recoveredAngle = Math.acos(cosAngle) * 180 / Math.PI; // Takes the arccosine of the cosine of 45 degrees and converts the result to degrees
console.log(recoveredAngle); // Outputs 44.99999999999999 (degrees)
The Math.acos() function is a static function, which means that you
can call it directly from the Math object without creating an instance
of the Math object. For example:
let x = 0.5;
let angle = Math.acos(x); // Calls the Math.acos() function directly from the Math object
console.log(angle); // Outputs 1.0471975511965979 (radians)
Summary
In this article, we explored the Math.acos() function in JavaScript,
which computes the arccosine (inverse cosine) of a given value. We
looked at a few examples of using this function in practice, such as
finding the angle between two vectors, solving a triangle, and
generating random points on a circle. The Math.acos() function is a
powerful tool in the JavaScript math toolkit, and can be used to solve a
variety of problems involving trigonometry and geometry.

![JavaScript Math.acos() Method [In-Depth Tutorial]](/javascript-math-acos/javascript-math-acos.jpg)