JavaScript Reference ยท Math
Math.asin(), acos(), atan() and atan2()
Reference for the inverse trigonometric functions asin, acos, atan and atan2: ranges, argument order, angles from coordinates and degree conversion.
The inverse trigonometric functions turn a ratio back into an angle. Math.asin(), Math.acos() and Math.atan() are the inverses of sine, cosine and tangent. Math.atan2(y, x) is the one you will use most: it computes the angle of a point or vector from its two coordinates and picks the correct quadrant.
All results are in radians. Multiply by 180 / Math.PI to get degrees.
Math.asin(x) Math.acos(x) Math.atan(x) Math.atan2(y, x)
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.asin(x) Math.acos(x) Math.atan(x) Math.atan2(y, x)
| Name | Description |
|---|---|
x (for asin, acos, atan) |
A number. asin and acos need a value from -1 to 1. |
y (for atan2) |
The vertical coordinate of the point. Listed first. |
x (for atan2) |
The horizontal coordinate of the point. |
Return value: an angle in radians. asin() returns -ฯ/2 to ฯ/2, acos() returns 0 to ฯ, atan() returns -ฯ/2 to ฯ/2, and atan2() returns -ฯ to ฯ. Arguments outside the domain of asin() or acos() produce NaN.
Exceptions: a BigInt argument throws a TypeError.
Usage notes
atan2(y, x)hasyfirst. WritingMath.atan2(x, y)is a very common bug that mirrors the angle.- Quadrants.
atan(y / x)cannot tell(1, 1)from(-1, -1), and fails whenxis0.atan2()handles both, and returnsMath.PI / 2for the point(0, 1). - Range. Negative results mean angles below the x axis. Add
2 * Math.PI(or 360 degrees) and use%to normalize into a full turn. - Screen coordinates have the y axis pointing down, so the "up" direction in the examples is negative
yon screen. - Rounding.
Math.asin(Math.sin(x))equalsxonly inside the function's range, and with a tiny error. - Domain errors. Slightly out-of-range values caused by rounding (such as
1.0000000000000002) giveNaNfromasin()andacos(). Clamp the input when it comes from a computed ratio. - Applications: aiming a turret at a target, rotating a sprite to face the pointer, computing bearing, polar coordinates.
Browser support
Supported in every browser and in Node.js.