trigonometry

Math.cos

Returns the cosine of an angle expressed in radians. Result is always in [-1, 1].

Math.cos(x)

Parameters & edge cases

Case Behavior
x Angle in RADIANS
range Returns a value in [-1, 1]
phase relation cos(x) === sin(x + π/2)

Examples

console.log(Math.cos(0));           // 1

cos(0) = 1 exactly.

console.log(Math.cos(Math.PI));    // -1

cos(π) = -1.

console.log(Math.cos(Math.PI / 2)); // 6.123233995736766e-17

Should be 0 but Math.PI is inexact.

console.log(Math.cos(60 * Math.PI / 180)); // 0.5000000000000001

cos(60°) — nearly exactly 0.5.

Gotcha

Like Math.sin(Math.PI), Math.cos(Math.PI / 2) is not exactly 0. Never use === when checking against expected trig values.

Related

← All JS Math methods · Array methods