totn JavaScript

JavaScript: Math cos() function

This JavaScript tutorial explains how to use the math function called cos() with syntax and examples.

Description

In JavaScript, cos() is a function that is used to return the cosine of a number. Because the cos() function is a static function of the Math object, it must be invoked through the placeholder object called Math.

Syntax

In JavaScript, the syntax for the cos() function is:

Math.cos(number);

Parameters or Arguments

number
The number used to calculate the cosine. It is the value of an angle expressed in radians.

Returns

The cos() function returns the cosine of a number. The result will be a value between 1 and -1.

Note

  • To convert degrees to radians, multiply by 2π/360 or 0.017453293.
  • Math is a placeholder object that contains mathematical functions and constants of which cos() is one of these functions.

Example

Let's take a look at an example of how to use the cos() function in JavaScript.

For example:

console.log(Math.cos(0.75));
console.log(Math.cos(-0.9));
console.log(Math.cos(2));

In this example, we have invoked the cos() function using the Math class.

We have written the output of the cos() function to the web browser console log, for demonstration purposes, to show what the cos() function returns.

The following will be output to the web browser console log:

0.7316888688738209
0.6216099682706644
-0.4161468365471424

In this example, the first output to the console log returned 0.7316888688738209 which is the cosine of 0.75.

The second output to the console log returned 0.6216099682706644 which is the cosine of -0.9.

The third output to the console log returned -0.4161468365471424 which is the cosine of 2.