Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Math.acos: inverse cosine

Math.acos returns the inverse cosine of a number in radians, with real domain -1 through 1 and result 0 through π.

Version: ECMAScript 1 onward; example target: Node.js 24.22.2

Syntax

Math.acos(x)

Math.acos(x) applies ToNumber, then computes the principal inverse cosine. For x in [-1, 1], the result lies in [0, π] radians: acos(1) is +0, acos(0) is approximately π/2, and acos(-1) is approximately π. The result is a Number and is generally an approximation.

A finite value outside [-1, 1], ±Infinity, NaN, or a missing argument produces NaN. A numeric string is converted, but a Symbol or BigInt throws TypeError; an object can invoke valueOf or toString. The specification permits an implementation-dependent approximation of the mathematical function.

The function is not a constructor and has been standard since ECMAScript 1. Its result is in radians; convert to degrees with radians * 180 / Math.PI. For an angle derived from a dot product, clamp the quotient to [-1, 1] so small floating-point error does not accidentally produce NaN.

Examples

function angleBetween([ax, ay], [bx, by]) {
  const quotient = (ax * bx + ay * by) / (Math.hypot(ax, ay) * Math.hypot(bx, by));
  return Math.acos(Math.max(-1, Math.min(1, quotient)));
}
const angle = angleBetween([1, 0], [0, 1]);
console.log(angle.toFixed(6), (angle * 180 / Math.PI).toFixed(1));
console.log(Math.acos(1), Math.acos(-1).toFixed(6));
console.log(Number.isNaN(Math.acos(1.1)), Number.isNaN(Math.acos()));
try { Math.acos(1n); } catch (error) { console.log(error instanceof TypeError); }

Result

1.570796 90.0
0 3.141593
true true
true

Sources