JavaScript Reference ยท Math
Math.sin(), Math.cos() and Math.tan()
Reference for Math.sin(), Math.cos() and Math.tan(): angles in radians, degree conversion, rounding artifacts, circles, waves and animation examples.
Math.sin(), Math.cos() and Math.tan() are the basic trigonometric functions. Each takes an angle and returns a number: the sine, cosine or tangent of that angle. They are the tools for anything that moves in circles or waves: clocks, orbits, pendulums, audio, and smooth animation.
The most important rule: angles are in radians, not degrees. A full turn is 2 * Math.PI radians (about 6.283) and a half turn is Math.PI. Convert degrees with degrees * Math.PI / 180.
Math.sin(angle) Math.cos(angle) Math.tan(angle)
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Math.sin(angle) Math.cos(angle) Math.tan(angle)
| Name | Description |
|---|---|
angle |
The angle in radians. Converted to a number first. |
Return value: for sin() and cos(), a number from -1 to 1; for tan(), any number. If angle is Infinity, -Infinity or NaN, the result is NaN.
Exceptions: a BigInt argument throws a TypeError.
Usage notes
- Degrees need conversion. Multiply by
Math.PI / 180. The opposite isradians * 180 / Math.PI. - Floating-point noise.
Math.sin(Math.PI)is about1.22e-16, becauseMath.PIis a rounded approximation of ฯ. Round for display, or compare using a tolerance. tan()near 90 degrees explodes:Math.tan(Math.PI / 2)is a huge finite number, notInfinity.- Circles. A point at angle
aand radiusrfrom a center is(cx + r * cos(a), cy + r * sin(a)). In screen coordinates the y axis points down, so angles run clockwise. - Waves and animation.
Math.sin(time * speed)gives smooth back-and-forth values between-1and1; rescale with(Math.sin(t) + 1) / 2to get0to1. - Periodic. The functions repeat every
2 * Math.PI, so very large arguments lose precision. - Order of arguments does not matter here, but for the inverse operation from coordinates to an angle use
Math.atan2(y, x).
Browser support
Supported in every browser and in Node.js.