Math.cos()
Returns the cosine of an angle given in radians, a value between -1 and 1.
Math.cos(x)- Added in JavaScript ES1
Parameters
| Parameter | Type | Description |
|---|---|---|
$x | number | A number representing an angle in radians. |
Return value
numberThe cosine of x, between -1 and 1, inclusive. If x is Infinity, -Infinity, or NaN, returns NaN.
Math.cos(x) takes an angle in radians and returns its cosine. It is Math.sin() shifted by a quarter turn: cosine starts at 1 when sine starts at 0.
When to reach for it
Cosine gives the horizontal component. Converting polar to Cartesian, x = r * Math.cos(θ) pairs with y = r * Math.sin(θ) to place points on a circle, lay out radial menus, or step a projectile along its heading.
Its other big use is similarity. The dot product of two vectors equals |a| |b| cos θ, so cosine measures alignment: 1 means the same direction, 0 perpendicular, -1 opposite. Cosine similarity between embedding vectors, Lambert shading in graphics, and easing curves such as (1 - Math.cos(t * Math.PI)) / 2 all rest on that identity.
What trips people up
Radians, again — Math.cos(180) is -0.598, not -1. And exactness is not guaranteed: Math.cos(Math.PI / 2) is 6.123233995736766e-17, not 0, because Math.PI is rounded. Compare with an epsilon. Non-finite input returns NaN.
Cosine is even — Math.cos(-x) equals Math.cos(x) — so it discards the sign of the angle and cannot tell you which side of the axis a point is on. Use Math.atan2() when direction matters.
Example
When to use it
- Place items evenly around a circle or radial menu
- Compute cosine similarity between two feature vectors
- Build a smooth ease-in-out curve from a normalized progress value
- Resolve a vector into its horizontal component from a heading
Frequently asked questions
Does Math.cos() use degrees or radians?
Math.cos() uses radians. Multiply degrees by Math.PI / 180 first — Math.cos(180) returns -0.598 because it reads 180 as radians, while Math.cos(180 * Math.PI / 180) returns -1.
How do I find the angle between two vectors in JavaScript?
Divide their dot product by the product of their magnitudes and pass the result to Math.acos(). Math.cos() is the forward direction of that relationship; Math.acos() recovers the angle in radians between 0 and π.
Why is Math.cos(Math.PI / 2) not exactly 0?
Math.PI is a double approximation of π, so Math.PI / 2 is slightly off from a true quarter turn and its cosine is about 6.12e-17. Use an epsilon comparison rather than checking for exact equality with 0.
More examples
Cosine similarity
The ratio of the dot product to the product of magnitudes is the cosine of the angle between the vectors: 1 for identical direction, 0.7071 for 45 degrees, 0 at a right angle and -1 when reversed. No call to Math.cos() is needed — the formula is its inverse in action.
function cosineSimilarity(a, b) {
const dot = a.reduce((s, v, i) => s + v * b[i], 0);
return dot / (Math.hypot(...a) * Math.hypot(...b));
}
console.log(cosineSimilarity([1, 0], [1, 0])); // identical
console.log(cosineSimilarity([1, 0], [1, 1]).toFixed(4));
console.log(cosineSimilarity([1, 0], [0, 1])); // perpendicular
console.log(cosineSimilarity([1, 0], [-1, 0])); // oppositeEase-in-out from a cosine
Mapping progress 0..1 onto half a cosine cycle produces 0, 0.1464, 0.5, 0.8536, 1 — slow at both ends and fastest in the middle. The curve stays within [0, 1] exactly because cosine is bounded by -1 and 1.
const easeInOut = (t) => (1 - Math.cos(t * Math.PI)) / 2;
for (let t = 0; t <= 1.0001; t += 0.25) {
console.log(t.toFixed(2), easeInOut(t).toFixed(4));
}Laying out a radial menu
Each item advances by a quarter turn, so cosine drives x and sine drives y to give (100,0), (0,100), (-100,0), (0,-100). Rounding absorbs the 1e-15 residue left by the inexact value of Math.PI.
function radialLayout(labels, radius) {
const step = (2 * Math.PI) / labels.length;
return labels.map((label, i) => ({
label,
x: Math.round(radius * Math.cos(i * step)),
y: Math.round(radius * Math.sin(i * step)),
}));
}
console.log(radialLayout(["N", "E", "S", "W"], 100));
Discussion