JavaScript Math.cos()
Syntax & Examples
Math.cos() static-method
The Math.cos() method in JavaScript returns the cosine of a number (x) given in radians. It is a static method of Math, meaning it is always used as Math.cos() and not as a method of a Math object created (Math is not a constructor).
Syntax of Math.cos()
The syntax of Math.cos() static-method is:
Math.cos(x)
This cos() static-method of Math returns the cosine of x.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
x | required | A number representing an angle in radians for which to return the cosine. |
Return Type
Math.cos() returns value of type Number
.
✐ Examples
1 Using Math.cos() with a positive number
In this example, we use the Math.cos()
method to find the cosine of 0 (radians).
For example,
- We call
Math.cos()
with 0 as the argument. - The method returns the cosine of 0.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.cos(0));
Output
1
2 Using Math.cos() with Pi
In this example, we use the Math.cos()
method to find the cosine of Pi radians.
For example,
- We call
Math.cos()
with Math.PI as the argument. - The method returns the cosine of Pi.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.cos(Math.PI));
Output
-1
3 Using Math.cos() with a negative number
In this example, we use the Math.cos()
method to find the cosine of -1 (radians).
For example,
- We call
Math.cos()
with -1 as the argument. - The method returns the cosine of -1.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.cos(-1));
Output
0.5403023058681398
Summary
In this JavaScript tutorial, we learned about cos() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.