The Wayback Machine - https://web.archive.org/web/20241218124641/https://www.geeksforgeeks.org/javascript-math-cos-method/
Open In App

JavaScript Math cos() Method

Last Updated : 15 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The Javascript Math.cos() method is used to return the cosine of a number. The Math.cos() method returns a numeric value between -1 and 1, which represents the cosine of the angle given in radians. The cos() is a static method of Math, therefore, it is always used as Math.cos(), rather than as a method of a Math object created.

Syntax: 

Math.cos(value)

Parameters:

This function accepts a single parameter as mentioned above and described below: 

  • Value: This parameter is a number given in radians.

Return Value:

  • The Math.cos() function returns the cosine of the given numbers which range from -1 to 1.

Example: This example returns the cosine of 0.

JavaScript
console.log("The cos of 0 : " + Math.cos(0));

Output
The cos of 0 : 1

Example 2: This example returns the cosine of 010.

JavaScript
console.log("Result : " + Math.cos(010));

Output
Result : -0.14550003380861354

Example 3: When 1 is passed as a parameter.

JavaScript
console.log("Result : " + Math.cos(1));

Output
Result : 0.5403023058681398

Example 4: When PI is passed as a parameter.  

JavaScript
console.log("Result : " + Math.cos(Math.PI));

Output
Result : -1

Example 5: When Input is in degrees (not in radians)

JavaScript
const degree = 90;
console.log(
"cos(" + degree + ") = " + Math.cos((Math.PI * degree) / 180));

Output
cos(90) = 6.123233995736766e-17

Supported Browsers:

  • Chrome 51
  • Edge 15
  • Firefox 54
  • Safari 10
  • Opera 38

JavaScript Math cos() Method – FAQs

What does the Math.cos() method do in JavaScript?

The Math.cos() method returns the cosine of a number, where the number is given in radians.

How do you convert degrees to radians for use with Math.cos()?

To convert degrees to radians, multiply the degree value by Math.PI / 180.

Can Math.cos() handle special numeric values like NaN, Infinity, and -Infinity?

  • Math.cos(NaN) returns NaN.
  • Math.cos(Infinity) and Math.cos(-Infinity) return NaN.

How does Math.cos() relate to the unit circle?

The Math.cos() method computes the x-coordinate of the point on the unit circle corresponding to the given angle in radians.

What are common use cases for the Math.cos() method?

  • Trigonometric Calculations: Using Math.cos() in mathematical and physics calculations involving angles.
  • Waveform Generation: Generating cosine waves for audio processing, signal processing, and simulations.
  • Graphics and Animations: Calculating positions and movements in computer graphics and animations.

We have a complete list of Javascript Math Objects methods, to check those please go through this Javascript Math Object Complete reference article.


Next Article

Similar Reads

three90RightbarBannerImg