JavaScript Math cos: Complete Guide
The Math.cos() function in Javascript is used to return the cosine of that number. This method comes in handy in programming contexts dealing with any trigonometric expressions.
JavaScript Math cos()
Javascript math cos() is a built-in function used to find the cosine value of the given number. Since cos() is a static method of Math, it can be used without creating an object.
The Javascript cos() method returns the numeric value between -1 and 1, representing the cosine of an angle given in radians.
To find the cosine value of a given argument in JavaScript, the Math.cos() method is used.
Syntax
Math.cos(x)
Parameter(s)
The variable x (in radians) whose cosine value is to be determined.
Return Value
The cosine value between -1 and 1.
See the following figure.
Note:
- If the passed value is empty, this method returns NaN.
Compatibility:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
- Android webview
- Chrome for Android
- Firefox for Android
- Opera for Android
- Safari on iOS
- Samsung Internet
- Node.js
JavaScript version: ECMAScript 1
Consider the following examples.
example1.js
The following code example demonstrates the use of the Javascript cos() method.
//example1.js let a = 0; let b = 1; let c = Math.PI; let d = 2 * Math.PI; console.log(Math.cos(a)); console.log(Math.cos(b)); console.log(Math.cos(c)); console.log(Math.cos(d));
Output
node example1 1 0.5403023058681398 -1 1
Pass empty value to the cos() method(example2.js)
The following example demonstrates the case where an empty value is passed.
// example2.js let x; console.log(Math.cos(x)); console.log(Math.cos());
Output
node example2 NaN NaN
Find the sides of the triangle using cos() method
The following code example demonstrates the application of this method in a simple programming context.
Given the base angle of a triangle and the base side, find all the remaining sides of the triangle.
// example3.js // Given the base angle of a triangle and the base side, // find all the remaining sides of the triangle. let p; let b; let h; let angle; const r = require('readline'); const rl = r.createInterface({ input: process.stdin, output: process.stdout }); const prompt1 = () => { return new Promise((resolve, reject) => { rl.question('Base side: ', (answer) => { b = answer; resolve(); }); }); }; const prompt2 = () => { return new Promise((resolve, reject) => { rl.question('Base angle(in radians): ', (answer) => { angle = answer; resolve(); }); }); }; const main = async () => { await prompt1(); await prompt2(); rl.close(); console.log('The three sides of the triangle are:'); console.log(b); //base side h = b / (Math.cos(angle)); console.log(h); //hypotenuse let angle2 = (Math.PI) / 2 - angle; p = h * Math.cos(angle2); console.log(p); //third side } main();
Output
Test Case 1: ->node example3 Base side: 6 Base angle(in radians): 0.523599 The three sides of the triangle are: 6 6.928204127882605 3.464103410351597 Test Case2: ->node example3 Base side: 30 Base angle(in radians): 0.785398 The three sides of the triangle are: 30 42.42639993882793 29.99999019615471
Conclusion
If you want to find the cosine value of the given number, then use Javascript math.cos() method.