Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách lấy cosin của một số trong JavaScript

Dùng Math.cos(x) với x tính bằng radian. Trả về giá trị [-1, 1].

Ví dụ

<html>
  <head>
    <title>Phương thức Math.cos trong JavaScript</title>
  </head>
  <body>
    <script>
      document.write("cos(90) = " + Math.cos(90) + "<br>"); // radian
      document.write("cos(30) = " + Math.cos(30) + "<br>");
      document.write("cos(-1) = " + Math.cos(-1) + "<br>");
      document.write("cos(2π) = " + Math.cos(2*Math.PI));
    </script>
  </body>
</html>