How to get the cosine of a number in JavaScript?

JavascriptWeb DevelopmentFront End Technology

In this tutorial, we will learn how to get the cosine of a number in JavaScript.

The cosine cos (x) function is a fundamental function used in trigonometry (the others being the cosecant, cotangent, secant, sine, and tangent). The cosine of an angle is commonly defined as the ratio between the lengths of the side of the triangle adjacent to the angle and the hypotenuse, i.e., cos x = (adjacent/hypotenuse).

Let theta be a counter-clockwise angle measured from the x-axis along the arc of the unit circle then the arc termination’s horizontal location called cos (theta).

Following are the methods used to find the cosine of a number in JavaScript −

Using Math.cos() Method

Math's methods and attributes are static and may be accessed by acquiring Math as an object instead of constructing it. The Math.cos() method produces a numeric value between -1 and 1, representing the cosine of the angle. Because cos() is a static Math function, you always use it as Math.cos() instead of as a method of a Math object you constructed (Math is not a constructor). The Math.cos() static method returns the cosine of the supplied angle, which must be specified in radians.



For finding the cosine of a value, the value is passed as an argument to the Math.cos() function.


In this example, we use the Math object with the Math.cos() method to find the cosine of the parameter.

To check the cos() function, we take the value1 variable, which takes an input of a positive degree value, and the cosine function finds its value. Next, we take the value2 variable that takes the input of a negative number. Then value3 variable accepts the input of zero.

Then value4 variable takes a decimal number as a parameter and returns its cosine value with the help of the Math.cos() method. And value5 variable takes the input as a string, and the cosine function recognizes that it’s not a number and returns the same.

<html> <body> <h3> Get the cosine of a number using <i>Math.cos()</i> method </h3> <p id = "root"> </p> </body> <script> let root = document.getElementById("root"); let value1 = Math.cos(90); root.innerHTML += "Math.cos(90) = " + value1 + "</br>"; let value2 = Math.cos(-5); root.innerHTML += "Math.cos(-5) = " + value2 + "</br>"; let value3 = Math.cos(0); root.innerHTML += "Math.cos(0) = " + value3 + "</br>"; let value4 = Math.cos(0.88); root.innerHTML += "Math.cos(0.88) = " + value4 + "</br>"; let value5 = Math.cos("String"); root.innerHTML += "Math.cos('String') = " + value5 + "</br>"; </script> </html>

Using the math.cos() Method

Here we use an external JavaScript library known as math.js to find the cosine of a number. A large library with mathematical functions, Math.js is compatible with JavaScript and Node.js. This library's extra feature is its flexible expression parser, which facilitates symbolic computing. Since it has numerous built-in functions and provides data types, including fractions, complex numbers, matrices, and units, it is highly powerful and simple to use.

It calculates a number's cosine. The data types of numbers, Big Number, Complex, and Unit, can be entered using this technique. Depending on the input, it returns an integer, a Big Number, or a Complex result.



When finding the cosine of a number, a value is passed as a parameter to the math.cos() function.


To get the cosine of the argument, we use the external library math.js and the math.cos() function.

The cosine function determines the value of the value1 variable, which accepts an input of a positive degree value. A negative number is entered into the value2 variable.

The value3 variable takes zero as an input. The value4 variable accepts a decimal integer as an argument and uses the Math.cos() function to return its cosine value. The value5 variable accepts a fraction as input and returns the cosine value.

<html> <body> <h3> Get the cosine of a number using math.js's <i>math.cos()</i> method </h3> <p id = "root"> </p> </body> <script src=""></script> <script> let root = document.getElementById("root"); let value1 = math.cos(180); root.innerHTML += "math.cos(180) = " + value1 + "</br>"; let value2 = math.cos(-8); root.innerHTML += "math.cos(-8) = " + value2 + "</br>"; let value3 = math.cos(0); root.innerHTML += "math.cos(0) = " + value3 + "</br>"; let value4 = math.cos(0.77); root.innerHTML += "math.cos(0.77) = " + value4 + "</br>"; let value5 = math.cos(1/4); root.innerHTML += "math.cos(1/4) = " + value5 + "</br>"; </script> </html>

In this tutorial, we covered two techniques for calculating the cosine of a value in radians. The first option is to utilize JavaScript's built-in Math functions. The second method is to utilize an external Mathematical library known as math.js and use the mathematical functions that this library provides to compute the cosine of an integer.

Updated on 06-Sep-2022 14:17:27