The JavaScript Cos function is one of the JavaScript Math function which is used to calculate the Trigonometry Cosine for the specified expression. In this article we will show you, How to use COS() function in JavaScript Programming language with example.
The mathematical formula behind the JavaScript Trigonometry Cosine function is as shown below:
COS(x) = Length of the Adjacent Side / Length of the Hypotenuse
TIP: Please refer JavaScript ACOS Function article to find the Arc Cosine of specified expression.
Syntax of a JavaScript Cos Function
The basic syntax of the JavaScript Cos Function is as shown below:
Math.cos(number);
Number: It can be a number or a valid numerical expression for which you want to find Cosine value.
- If the number argument is positive or negative number, cos() function will return the Cosine value.
- If the number argument is not a number, cos() function will return NaN.
NOTE: The Cos() function will return the value between -1 and 1.
JavaScript Cos Function Example
The JavaScript Cos Function allows you to find the trigonometry Cosine for the numeric values.
In this example, We are going to find the Cosine values of different data types and display the output
JAVASCRIPT CODE
<!DOCTYPE html> <html> <head> <title> JavaScript COS Function </title> </head> <body> <h1> JavaScript COS Function </h1> <p id = "Pos"></p> <p id = "Neg"></p> <p id = "Dec"></p> <p id = "Neg_Dec"></p> <p id = "Str"></p> <p id = "Exp"></p> <p id = "Null"></p> <p id = "Multi"></p> <script> document.getElementById("Pos").innerHTML = Math.cos(20); document.getElementById("Neg").innerHTML = Math.cos(-10); document.getElementById("Dec").innerHTML = Math.cos(40.45); document.getElementById("Neg_Dec").innerHTML = Math.cos(-60.75); document.getElementById("Str").innerHTML = Math.cos("JavaScript"); document.getElementById("Null").innerHTML = Math.cos(null); document.getElementById("Multi").innerHTML = Math.cos(0.25 + 0.55 - 0.77); </script> </body> </html>
OUTPUT
ANALYSIS
First, We used the JavaScript COS() Function directly on both the Positive integer and negative integer.
document.getElementById("Pos").innerHTML = Math.cos(20); document.getElementById("Neg").innerHTML = Math.cos(-10);
Next, We used the COS() Function on both the Positive and negative Decimal values. If you observe the above screenshot, Cos() function will work perfectly with decimal values as well
document.getElementById("Dec").innerHTML = Math.cos(40.45); document.getElementById("Neg_Dec").innerHTML = Math.cos(-60.75);
Next, We tried COS() Function on String value. As we said before, this will return NaN (Not a Number) as output
document.getElementById("Str").innerHTML = Math.cos("JavaScript");
Next, We tried COS() Function on Null value and it return One as output
document.getElementById("Null").innerHTML = Math.cos(null);
In the below statement, We used the JavaScript COS() Function directly on multiple values.
document.getElementById("Multi").innerHTML = Math.cos(0.25 + 0.55 - 0.77);
Thank You for Visiting Our Blog