How to Use the Math.cos() Function in JavaScript?

Estimated read time 1 min read

The Math.cos() function in JavaScript returns the cosine of a number. The cosine of a number is defined as the ratio of the adjacent side to the hypotenuse of a right-angled triangle.

Here’s an example of how to use the Math.cos() function:

let angle = Math.PI / 4;
let cos = Math.cos(angle);

console.log(cos);

This will output 0.7071067811865475, which is the cosine of Math.PI / 4. The value of Math.PI is a constant in JavaScript that represents the mathematical constant pi.

Note that the Math.cos() function takes an angle in radians as its argument. To convert an angle from degrees to radians, you can use the following formula:

let radians = degrees * (Math.PI / 180);

So, for example, to find the cosine of an angle of 30 degrees, you can do the following:

let degrees = 30;
let radians = degrees * (Math.PI / 180);
let cos = Math.cos(radians);

console.log(cos);

This will output 0.8660254037844386, which is the cosine of 30 degrees in radians.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply