C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Math cos() methodThe JavaScript math cos() method returns the cosine of the given number. The value returned by cos() method ranges -1 to 1, which represents the cosine of the angle. SyntaxThe cos() method is represented by the following syntax: Math.cos(num) Parameternum - A number. ReturnThe cosine of a number. JavaScript Math cos() method exampleHere, we will understand cos() method through various examples. Example 1Let's see a simple example of cos() method. <script> document.writeln(Math.cos(1)+"<br>"); document.writeln(Math.cos(2)); </script> Output: 0.5403023058681398 -0.4161468365471424 Example 2In this example, we will use cos() method with negative numbers. <script> document.writeln(Math.cos(-1)+"<br>"); document.writeln(Math.cos(-0.5)); </script> Output: 0.5403023058681398 0.8775825618903728 Example 3Let's see cos() method with some different test cases. <script> document.writeln(Math.cos(0)+"<br>"); document.writeln(Math.cos(Math.PI)); </script> Output: 1 -1 Example 4Here, you can test cos() method with your own test cases. <script> function display() { var x=document.getElementById("num").value; document.getElementById("result").innerHTML=Math.cos(x); } </script> <form> Enter a number: <input type="text" id="num"> <input type="button" onclick="display()" value="submit"> </form> <p><span id="result"></span></p>
Next TopicJavaScript Math
|