Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
Math.sin(angle)
Math.cos(angle)
Math.tan(angle)

Try it

JS Radians, not degrees

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Points on a circle

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 is radians * 180 / Math.PI.
  • Floating-point noise. Math.sin(Math.PI) is about 1.22e-16, because Math.PI is 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, not Infinity.
  • Circles. A point at angle a and radius r from 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 -1 and 1; rescale with (Math.sin(t) + 1) / 2 to get 0 to 1.
  • 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.