JavaScript Math.cos() returns the cosine of an angle. Searches such as javascript cosine, javascript math cos, javascript cos, math cos js, and js cos all refer to the same method. The important detail is that Math.cos() expects the angle in radians, not degrees.
Environment: Node.js v20.18.2. Each snippet is plain JavaScript; the line after it states the expected console output.
Math.cos Syntax
Math.cos(angleInRadians)The return value is a number between -1 and 1.
Method 1: Calculate Cosine in Radians
console.log(Math.cos(0));
console.log(Math.cos(Math.PI));You should see 2 lines, in order: 1, -1.
The cosine of 0 radians is 1, and the cosine of π radians is -1.
Method 2: Convert Degrees to Radians Before Math.cos()
To use degrees, convert them to radians first.
const degrees = 60;
const radians = degrees * (Math.PI / 180);
console.log(Math.cos(radians));You should see one line logging 0.5000000000000001.
The expected value is 0.5; the tiny difference is normal floating-point behavior.
Method 3: Generate Cosine Wave Values
const values = [0, Math.PI / 2, Math.PI].map((angle) => {
return Number(Math.cos(angle).toFixed(6));
});
console.log(values.join(","));You should see one line logging 1,0,-1.
Cosine is useful for waves, circular motion, animation, physics, and graphics.
Common Questions About JavaScript Math.cos
Does Math.cos use degrees or radians?
Math.cos() uses radians. Convert degrees with degrees * Math.PI / 180.
What does Math.cos return?
It returns the cosine of the input angle as a number between -1 and 1.
Why is Math.cos(60) not 0.5?
Because 60 is treated as radians, not degrees. Convert 60 degrees to radians first.
Summary
Math.cos() always reads the angle in radians; convert degrees with deg * Math.PI / 180 before calling.
JavaScript Math.cos() calculates cosine for an angle in radians. Use it for trigonometry, waves, circular motion, and graphics. When working with degree values such as 60 degrees, convert to radians before calling Math.cos(). Results may contain tiny floating-point differences, which are normal in JavaScript number calculations.
