JavaScript(JS) math method - cos
The cos
method in JavaScript's Math
object returns the cosine of an angle, specified in radians.
The cosine of an angle is defined as the ratio of the adjacent side to the hypotenuse in a right-angled triangle containing the angle. In a unit circle, the cosine of an angle is the x-coordinate of the point on the circumference that is intersected by a line drawn from the center of the circle to the point on the circumference that corresponds to the angle.
The cos
method takes one argument, which is the angle in radians. If the argument is not a number, it will be converted to one before the calculation. If the argument is NaN, NaN
will be returned.
Here's an example usage:
const x = Math.PI / 4; // 45 degrees in radians const cosX = Math.cos(x); console.log(cosX); // 0.7071067811865476
In the above example, the Math.cos
method is used to calculate the cosine of an angle of 45 degrees, which is first converted to radians using Math.PI / 4
. The result, which is approximately 0.7071067811865476, is then logged to the console.