JavaScript Math cos()

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

Example


cos() Syntax

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

Math.cos(angle)

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()

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

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

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

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: