What is Math.cos() in JavaScript?
In JavaScript, Math is a built-in object that contains different methods and properties used to perform mathematical operations. It contains a cos() function, which is used to compute the cosine of a specified angle.
Syntax
Math.cos(param);
Parameter
param: This is the angle for which we want to compute the cosine. Its type isNumber, and it is measured in radians.
Numberin JavaScript is a 64-bit double-precision value which is the same asdoublein C# or Java.
Return value
-
Number: It returns the cosine of the given angle stored inparam. It is of theNumbertype. Its range is:- -1 <= result <= 1
-
NaN: The function returnsNaNifparamis positive or negativeInfinity.
Use the following formula to convert degrees to radians:
- Radians = Degrees x
π/180where
π= 3.14159, approximately.
Example
console.log("cos(-1) = " + Math.cos(-1));console.log("cos(0) = " + Math.cos(0));console.log("cos(1) = " + Math.cos(1));console.log("cos(∞) = " + Math.cos(Infinity));console.log("cos(-∞) = " + Math.cos(-Infinity));