JavaScript Math cos()

The JavaScript Math.cos() function returns the cosine of a specified number.

The syntax of the Math.cos() function is:


cos(), being a static method, is called using the Math class name.

Math.cos() Parameters

The Math.cos() function takes in:

  • x - A number (in radians) whose cosine value is required.

Return value from Math.cos()

  • Returns the cosine of a given angle (a numeric value between -1 and 1).

Example 1: Using Math.cos()

// cosine of 1 radian
var value1 = Math.cos(1);
console.log(value1); // Output: 0.5403023058681398

// negative radians are allowed
var value2 = Math.cos(-2);
console.log(value2); // Output: -0.4161468365471424

// Math constants can be used
var value3 = Math.cos(Math.PI);
console.log(value3); // Output: -1



Example 2: Using Math.cos() with degrees

// custom function for angle in degrees
function cos(degrees) {
  var radians = (degrees * Math.PI) / 180;
  return Math.cos(radians);

// cosine of 57 degrees
value1 = cos(57);
console.log(value1); // Output: 0.5446390350150272

// cosine of negative degrees
value2 = cos(-180);
console.log(value2); // Output: -1

value3 = cos(360);
console.log(value3); // Output: 1



Here, we have defined a cos() function that converts the degree value to radian and then passes it to Math.cos().

We can define custom functions in similar ways to extend the capabilities of such built-in functions.

Recommended readings:

Did you find this article helpful?