JavaScript Math.cos() Method

Definition
Math.cos(x) returns the cosine of an angle. The result is always between -1 and 1. The angle must be in radians, not degrees.
Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().
Syntax
Call Math.cos() like this. Start with this line.
| Call | What it does |
|---|
Math.cos(x) | Returns the cosine of x, where x is in radians. |
Built into JavaScript. Call Math.cos() on the Math object — no import required.
Library declarations
interface Math {
cos(x: number): number;
}
Parameters
| Parameter | Description |
|---|
| x | Required. An angle in radians. Convert degrees with degrees * Math.PI / 180. |
Return Value
| Return Value | Description |
|---|
| number | The cosine of x, from -1 to 1. Returns NaN if x is Infinity, -Infinity, or not a number. |
JavaScript Examples
Example 1 — Basic Use
Pass an angle in radians. Math.PI is half a circle.
console.log(Math.cos(0)); // 1
console.log(Math.cos(Math.PI)); // -1
console.log(Math.cos(2 * Math.PI)); // 1
console.log(Math.cos(Math.PI / 2)); // 6.123233995736766e-17
1
-1
1
6.123233995736766e-17
How It Works
- Zero angle.
Math.cos(0) is 1. - Half circle.
Math.PI is 180 degrees, so the cosine is -1. - Full circle.
2 * Math.PI is back at the start, so 1. - Tiny number. The cosine of 90 degrees should be
0, but floating-point math gives a very small number instead.
Example 2 — Degrees to Radians
Math.cos() wants radians. Use a helper to convert degrees first.
function toRadians(degrees) {
return degrees * Math.PI / 180;
}
console.log(Math.cos(toRadians(60)).toFixed(2)); // 0.50
console.log(Math.cos(toRadians(90)).toFixed(2)); // 0.00
console.log(Math.cos(toRadians(180)).toFixed(2)); // -1.00
How It Works
- Convert.
degrees * Math.PI / 180 changes degrees into radians. - Call cos. Pass the radians to
Math.cos. - Round.
toFixed(2) hides the tiny error, so 90 degrees shows 0.00.
Example 3 — Point on a Circle
Use cos for the x position and sin for the y position.
function pointOnCircle(radius, degrees) {
var angle = degrees * Math.PI / 180;
return {
x: radius * Math.cos(angle),
y: radius * Math.sin(angle)
};
}
var p = pointOnCircle(10, 90);
console.log(p.x.toFixed(2)); // 0.00
console.log(p.y.toFixed(2)); // 10.00
var q = pointOnCircle(10, 0);
console.log(q.x.toFixed(2)); // 10.00
How It Works
- Convert the angle. Turn degrees into radians first.
- x uses cos.
radius * Math.cos(angle) is the horizontal position. - y uses sin.
radius * Math.sin(angle) is the vertical position. - Check. At 90 degrees the point is straight up:
x is 0, y is 10.
Compatibility
Browser & Runtime Support
Math.cos() has been part of JavaScript since ES1 and is Baseline Widely available everywhere.
✓ Baseline · ES1
Math.cos()
Supported in every browser and Node.js version. No polyfill is needed.
99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full supportMath.cos Excellent
Bottom line: Use Math.cos() freely. It works in all browsers and runtimes.
Frequently Asked Questions
Math.cos(x) returns the cosine of the angle x. The angle is in radians. Math.cos(0) is 1 and Math.cos(Math.PI) is -1.
Radians. To use degrees, convert first: Math.cos(degrees * Math.PI / 180).
Math.PI is not perfectly exact, so you get a tiny number like 6.123233995736766e-17. Use toFixed() or round the answer to hide it.
Always between -1 and 1.
Use x = radius * Math.cos(angle) and y = radius * Math.sin(angle), with the angle in radians.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful