JavaScript Math.acos(): Arccosine Examples in Radians and Degrees

JavaScript Math.acos(): arccosine in radians, valid domain -1 to 1, NaN outside that range, degrees conversion, and a simple vector angle example.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

JavaScript Math.acos(): Arccosine Examples in Radians and Degrees

JavaScript Math.acos() returns the arccosine (inverse cosine) of a number: the angle in radians whose cosine equals the argument. The input must lie in [-1, 1]; outside that range the result is NaN. The output is always in [0, π] radians—convert to degrees when presenting to humans. For the inverse sine, see JavaScript Math.asin(); for angles from x/y pairs prefer JavaScript Math.atan2.

Environment: Node.js v20.18.2. Each snippet is plain JavaScript; the line after it states the expected console output.


Unit circle diagram illustrating arccosine as the angle from the x-axis to a point on the circle with domain between negative one and one

Math.acos Syntax

javascript
Math.acos(x)
Output

x must be between -1 and 1. The return value is in radians.


Method 1: Calculate arccosine with Math.acos()

javascript
const angle = Math.acos(0.5);

console.log(angle);
Output

You should see one line logging 1.0471975511965979.

The result is approximately π / 3 radians because the cosine of π / 3 is 0.5.


Method 2: Convert Math.acos Result to Degrees

JavaScript trigonometric functions return radians. Convert radians to degrees by multiplying by 180 / Math.PI.

javascript
const radians = Math.acos(0.5);
const degrees = radians * (180 / Math.PI);

console.log(degrees);
Output

You should see one line logging 60.00000000000001.

The tiny decimal difference is normal floating-point behavior.


Method 3: Handle Values Outside -1 to 1

Math.acos() returns NaN when the input is outside the valid domain.

javascript
console.log(Math.acos(2));
Output

You should see one line logging NaN.

Validate or clamp input values when they come from user input or floating-point calculations.


Method 4: Find the Angle Between Two Vectors

You can use Math.acos() with the dot product formula to find the angle between vectors.

javascript
const a = [1, 0];
const b = [0, 1];

const dot = a[0] * b[0] + a[1] * b[1];
const magnitude = Math.hypot(...a) * Math.hypot(...b);
const degrees = Math.acos(dot / magnitude) * (180 / Math.PI);

console.log(degrees);
Output

You should see one line logging 90.

The vectors point along the x-axis and y-axis, so the angle between them is 90 degrees.


Common Questions About Math.acos

What is acos in math?

acos is arccosine, the inverse cosine function. It returns the angle whose cosine equals the input value.

Does Math.acos return degrees?

No. Math.acos() returns radians. Convert to degrees with radians * 180 / Math.PI.

Why does Math.acos return NaN?

It returns NaN when the input is less than -1 or greater than 1.


Summary

Math.acos() maps cosines in [-1, 1] to angles in [0, π] radians; outside that domain you get NaN—validate inputs from physics or UI before calling.

JavaScript Math.acos() calculates arccosine and returns an angle in radians. Use it for inverse cosine calculations, vector angles, and trigonometry problems where the cosine value is known. The input must be in the range -1 to 1; values outside that domain return NaN. For user-facing math output, convert the radians result to degrees.


Official Documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …