JavaScript Math cos()

The cos() method computes the trigonometric cosine of the specified angle and returns it.


// cosine of the angle 1
let value = Math.cos(1);

// Output: 0.5403023058681398

cos() Syntax

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


Here, cos() is a static method. Hence, we are accessing the method using the class name, Math.

cos() Parameter

The cos() method takes a single parameter:

  • angle - in radians whose cosine value is to be calculated

cos() Return Value

The cos() method returns:

  • cosine value of the given angle (in radians)
  • NaN (Not a Number) for a non-numeric argument

Example 1: JavaScript Math.cos()

// cosine of 5 radians let value1 = Math.cos(5);
// negative radians are allowed let value2 = Math.cos(-2);
console.log(value2); // Output: // 0.28366218546322625 // -0.4161468365471424

In the above example,

  • Math.cos(5) - computes the cosine value of the angle 5
  • Math.cos(-2) - computes the cosine value of the angle -2

Example 2: Math.cos() with Math Constants

// math constants as arguments let value3 = Math.cos(Math.PI);
console.log(value3); // Output: -1

In the above example, we have used the cos() method to compute the cosine of the math constant PI.

Example 3: Math.cos() with Non-Numeric argument

let string = "Darth Vader";

// cos() with string as argument let value = Math.cos(string);
console.log(value); // Output: // NaN

In the above example, we have tried to calculate the cosine value of the string "Darth Vader" resulting in NaN as the output.

Example 4: Math.cos() with Infinity

// infinity as argument let value1 = Math.cos(Infinity);
// negative infinity as argument let value2 = Math.cos(-Infinity);
console.log(value2); // Output: // NaN // NaN

The cos() method doesn't treat infinity as a number so the method returns NaN with this argument.

Also, the cosine of an infinite angle is indefinite, which can't be defined with a number.

Recommended readings:

Did you find this article helpful?