Home Communities
IT Knowledge
Inspiration
Languages
EN

# JavaScript - Math.cos() method example

15 points
Created by:
684

The `Math.cos()` function returns the cosine of the specified angle in radians in the range `-1` to `+1`.

``````// ONLINE-RUNNER:browser;

console.log( Math.cos( 0                  ) ); //   1 <-     0 degrees
console.log( Math.cos( 1.5707963267948966 ) ); //  ~0 <-  ~90 degrees ==  PI / 2
console.log( Math.cos( 3.1415926535897932 ) ); // ~-1 <- ~180 degrees ==  PI
console.log( Math.cos( 4.71238898038469   ) ); //  ~0 <- ~270 degrees == -PI * (3 / 2)
console.log( Math.cos( 6.2831853071795850 ) ); //  ~1 <- ~360 degrees ==  PI * 2

console.log( Math.cos(-1.5707963267948966 ) ); //  ~0 <- ~-90 degrees == -PI / 2``````

Note: `6.123233995736766e-17`, `-1.8369701987210297e-16` and `6.123233995736766e-17` should be equal to `0` but they are not because of computation precision error.

## 1. Documentation

 Syntax `Math.cos(number)` Parameters `number` - integer or float number value in radians (primitive value). Result returns a numeric value between `-1` and `+1`, which represents the cosine of the angle (primitive value). Description `cos` is static method that takes only one parameter and returns approximation of `cos(x)` mathematical function result.

## 2. Working with radians

``````// ONLINE-RUNNER:browser;

var x1 = 0.0;          // beginning of calculation in radians
var x2 = Math.PI / 2;  // ending of calculation radians

var dx = Math.PI / 36; // calculation step in degrees

for (var rad = x1; rad <= x2; rad += dx) {
var y = Math.cos(rad);

console.log('cos(' + rad + ' rad) = ' + y);
}``````

## 3. Working with degrees

``````// ONLINE-RUNNER:browser;

function calculateCos(deg) {
var rad = (Math.PI / 180) * deg;
}

// Usage example:

var x1 = 0.0;  // beginning of calculation in degrees
var x2 = 90.0; // ending of calculation degrees

var dx = 5.0;  // calculation step in degrees

for (var deg = x1; deg  <= x2; deg  += dx) {
var y = calculateCos(deg );

console.log('cos(' + deg + ' deg) = ' + y);
}``````

## 4. Reversed console plot example

``````// ONLINE-RUNNER:browser;

var x1 = 0.0;     // beginning of cosine chart
var x2 = 6 * 3.14 // end of cosine chart

var dx = 3.14 / 4.0; // x axis step
var dy = 1.0  / 5.0; // y axis step

for (var rad = x1; rad < x2; rad += dx) {
var y1 = 0.0;
var y2 = Math.cos(rad) + 1;

var line = '';
for(var y = y1; y < y2; y += dy) {
line += ' ';
}

console.log(line + '+');
}``````

## 5. Canvas plot example

``````// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<style> #canvas { border: 1px solid black; } </style>
<body>
<canvas id="canvas" width="400" height="130"></canvas>
<script>

function Chart(canvas, x1, x2, y1, y2, dx, func) {
var self = this;

var context = canvas.getContext('2d');

var width = canvas.width;
var height = canvas.height;

var xRange = x2 - x1;
var yRange = y2 - y1;

var calculatePoint = function(x) {
var y = func(x);
// chart will be reversed horizontaly because of reversed canvas pixels
var nx = (x - x1) / xRange;       // normalized x
var ny = 1.0 - (y - y1) / yRange; // normalized y
return {
x: nx * width,
y: ny * height
};
};

self.drawChart = function() {
var point = calculatePoint(x1);
context.beginPath();
context.moveTo(point.x, point.y);
for (var x = x1 + dx; x < x2; x += dx) {
var point = calculatePoint(x);
context.lineTo(point.x, point.y);
}
var point = calculatePoint(x2);
context.lineTo(point.x, point.y);
context.stroke();
};
};

// Usage example:

var canvas = document.querySelector('#canvas');

// cosine chart range
var x1 =  0;           // 0 degress
var x2 = +2 * Math.PI; // +360 degress
var y1 = -1.0;
var y2 = +1.0;

var dx = 0.1;

var chart = new Chart(canvas, x1, x2, y1, y2, dx, Math.cos);

chart.drawChart();

console.log('x range: <' + x1 + '; ' + x2 + '> // angles in radians');
console.log('y range: <' + y1 + '; ' + y2 + '>');

</script>
</body>
</html>``````

## References

1. Cosine - Wikipedia

## Alternative titles

Join to our subscribers to be up to date with content, news and offers.