JavaScript Math.cos() Method

Shubham Vora Sep 19, 2022
  1. Syntax
  2. Parameters
  3. Returns
  4. Example Codes
JavaScript Math.cos() Method

In maths, the range of the cosine function is between -1 to 1. Therefore, users can use the Math.cos() method to perform some mathematics operations where they need the cosine of an angle.

Syntax

let angle = 2;
Math.cos(angle);

Parameters

angle It is an angle in radians for which we need to find the cosine value.

Returns

The Math.cos() method returns the cosine value of an angle.

Example Codes

Let’s learn the use of Math.cos() using different example codes below.

Use the Math.cos() Method to Get the Cosine of Various Angles

The following code takes two different angle values in the radians. One of them is PI, and we know that cos(PI) is always equal to -1, which is what exactly Math.cos(PI) returns.

let angle1 = 5.2;
let angle2 = Math.PI;

console.log(Math.cos(angle1));
console.log(Math.cos(angle2));

Output:

0.4685166713003771
-1

Use the Math.cos() With Different Arithmetic Operators

The following code uses the Math.cos() method to find the cosine of the angle and perform some arithmetic operations with the cosine value.

From this, users can learn how to use the Math.cos() to perform a different operation, such as finding the side of the triangle, etc.

let angle1 = 2.3;
let value = 3; 

console.log(Math.cos(angle1)*3);
console.log(Math.cos(angle1)+value);
console.log(Math.cos(angle1)/value);

Output:

-1.998828063839472
2.333723978720176
-0.22209200709327467

The Math.cos() method is compatible with all modern browsers, and users can use it to find the cosine value of an angle.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub

Related Article - JavaScript Math