JavaScript Math.cos() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

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.

JavaScript
Math.cos(x)
CallWhat 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
JavaScript
Math.cos(x)
JavaScript
interface Math {
  cos(x: number): number;
}

Parameters

ParameterDescription
xRequired. An angle in radians. Convert degrees with degrees * Math.PI / 180.

Return Value

Return ValueDescription
numberThe 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.

JavaScript
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
Try It Yourself

How It Works

  1. Zero angle. Math.cos(0) is 1.
  2. Half circle. Math.PI is 180 degrees, so the cosine is -1.
  3. Full circle. 2 * Math.PI is back at the start, so 1.
  4. 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.

JavaScript
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
Try It Yourself

How It Works

  1. Convert. degrees * Math.PI / 180 changes degrees into radians.
  2. Call cos. Pass the radians to Math.cos.
  3. 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.

JavaScript
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
Try It Yourself

How It Works

  1. Convert the angle. Turn degrees into radians first.
  2. x uses cos. radius * Math.cos(angle) is the horizontal position.
  3. y uses sin. radius * Math.sin(angle) is the vertical position.
  4. Check. At 90 degrees the point is straight up: x is 0, y is 10.

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 support
Math.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.

Next: cosh()

Learn how to find the hyperbolic cosine.

cosh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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