The Math cos() method is used to calculate the cosine of a number. The input number should be in radians, not degrees.
Syntax
Math.cos(x)
Parameters
x(required): It is a number whose sine value is to be determined.
Return Value
Returns a numeric value between -1 and 1.
If the parameter is not numeric or if an empty value is passed, it returns NaN.
Visual Representation
Example 1: How to Use Math cos() Method
let a = 1; //positive value
let b = 4.8; //float value
let c = -4; //negative value
let d = 0;
console.log(Math.cos(a));
console.log(Math.cos(b));
console.log(Math.cos(c));
console.log(Math.cos(d));
Output
0.5403023058681398
0.0874989834394464
-0.6536436208636119
1
Example 2: Passing an empty and non-numeric value
console.log(Math.cos()); //empty value
console.log(Math.cos("AppDividend")); //non-numeric value
Output
NaN
NaN
Example 3: Passing PI as an argument
console.log(Math.cos(Math.PI));
console.log(Math.cos(Math.PI / 4)); //45 degrees
Output
-1
0.7071067811865476
Browser compatibility
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Opera 3 and above
- Safari 1 and above