Javascript Reference - JavaScript cos() Method








The cos() method returns the cosine of a number.

Browser Support

cos() Yes Yes Yes Yes Yes

Syntax

Math.cos(x);

Parameter Values

Parameter Description
x Required. A number




Return Value

The cos() method returns a numeric value between -1 and 1, which represents the cosine of the angle.

Example

The following code shows how to calculate the cosine of a number.

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
  function doMath() {<!-- ww w. j av a2s.  co m-->
    var inputNum = document.form1.input.value;
    var result = Math.cos(inputNum);
    document.form1.answer.value = result;
  }
</script>
</head>
<body>
  <form name=form1>
    <br> Enter First Number: 
    <input type="text" name="input" size=10>
    <input type="button" value="Calculate" onClick='doMath()'> 
    <br>
    The cosine is: <input type="text" name="answer" size=10>
  </form>
</body>
</html>

Click to view the demo