Math.cos
Returns the cosine of a number in radians
Syntax
Math.cos(x)Parameters
| Parameter | Type | Description |
|---|---|---|
| x | number | An angle in radians |
Return Value
The cosine of x, between -1 and 1
Examples
console.log(Math.cos(0)); // 1
console.log(Math.cos(Math.PI)); // -1function polarToCartesian(r: number, theta: number) {
return { x: r * Math.cos(theta), y: r * Math.sin(theta) };
}
console.log(polarToCartesian(1, 0)); // { x: 1, y: 0 }const angle = Math.PI / 3; // 60 degrees
console.log(Math.cos(angle)); // 0.5Understanding Math.cos
The Math.cos method in JavaScript returns the cosine of a number in radians. It belongs to the Math object and is one of the most widely used methods for working with math values in modern JavaScript and TypeScript applications.
The method signature is Math.cos(x). It accepts 1 parameter: x. When called, it returns the cosine of x, between -1 and 1. Understanding when and how to use cos() helps you write more expressive, readable code.
Common use cases for Math.cos include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like math-sin, math-tan, math-pi, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Math.cos is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Math Methods
Other methods in the Math object
Related Tools
More Math Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.