Math.asin()
Returns the arcsine of a number in radians, the inverse of Math.sin().
Math.asin(x)- Added in JavaScript ES1
Parameters
| Parameter | Type | Description |
|---|---|---|
$x | number | A number between -1 and 1, inclusive, representing the angle's sine value. |
Return value
numberThe inverse sine — an angle in radians between -π/2 and π/2, inclusive — of x. If x is less than -1 or greater than 1, returns NaN.
Math.asin(x) undoes Math.sin(): given a sine value it returns the angle, in radians, whose sine is that value. The result always lies in [-π/2, π/2] — the right half of the circle.
When to reach for it
The classic case is a right triangle where you know the opposite side and the hypotenuse: Math.asin(opposite / hypotenuse) gives the angle. It also appears in the haversine formula for great-circle distance, in converting a normalized height back into an angle for animation, and in extracting a pitch angle from a rotation matrix.
What trips people up
The domain is only [-1, 1]. Anything outside returns NaN without throwing, and a ratio that should be exactly 1 often comes out as 1.0000000000000002 after a division — that rounding alone is enough to poison the result. Clamp before calling: Math.asin(Math.min(1, Math.max(-1, ratio))).
The restricted range also loses information. Both 30° and 150° have a sine of 0.5, but Math.asin(0.5) can only return the 30° branch. When you need a full-circle angle, use Math.atan2(), which sees both coordinates and covers all four quadrants.
Example
When to use it
- Recover an angle from the opposite side and hypotenuse of a right triangle
- Finish a haversine great-circle distance calculation
- Convert a normalized height back into a phase angle
- Extract a pitch angle from a direction vector
Frequently asked questions
Why does Math.asin() return NaN?
Math.asin() returns NaN whenever its argument is outside [-1, 1], since no real angle has a sine beyond that range. Ratios computed by division often overshoot 1 by a rounding step, so clamp with Math.min(1, Math.max(-1, x)) before calling.
How do I convert the result of Math.asin() to degrees?
Multiply by 180 / Math.PI. Math.asin(0.6) returns 0.6435011087932844 radians, which is 36.87 degrees.
Should I use Math.asin() or Math.atan2() to find an angle?
Use Math.atan2(y, x) when you have both coordinates, because it returns an angle across the full circle from -π to π and never divides by zero. Math.asin() only covers -π/2 to π/2 and cannot tell 30 degrees from 150 degrees.
More examples
Clamping the domain
Accumulated rounding pushes the ratio a hair past 1, and the unguarded call returns NaN. Clamping first yields the intended π/2 = 1.5707963267948966. Out-of-range inputs then saturate at the endpoints instead of poisoning the result.
const clamp = (v) => Math.min(1, Math.max(-1, v));
const safeAsin = (v) => Math.asin(clamp(v));
const ratio = (0.1 + 0.2) / 0.3; // just over 1
console.log(ratio, ratio > 1);
console.log(Math.asin(ratio));
console.log(safeAsin(ratio));
console.log(safeAsin(-1.5), safeAsin(0.5));Haversine distance
London to Paris comes out at about 343.6 km. The Math.min(1, ...) guard matters for antipodal points, where the square root can round just above 1 and turn the whole distance into NaN.
const R = 6371; // km
const rad = (d) => (d * Math.PI) / 180;
function haversine(lat1, lon1, lat2, lon2) {
const dLat = rad(lat2 - lat1), dLon = rad(lon2 - lon1);
const a = Math.sin(dLat / 2) ** 2 +
Math.cos(rad(lat1)) * Math.cos(rad(lat2)) * Math.sin(dLon / 2) ** 2;
return 2 * R * Math.asin(Math.min(1, Math.sqrt(a)));
}
console.log(haversine(51.5074, -0.1278, 48.8566, 2.3522).toFixed(1) + " km");The lost branch
Round-tripping 150 degrees through sin and asin returns 30, because both angles share a sine of 0.5 and asin can only report the [-90, 90] branch. Math.atan2(), given both components, distinguishes 30 from 150.
const deg = (r) => Math.round((r * 180) / Math.PI);
console.log(deg(Math.asin(Math.sin(Math.PI / 6)))); // 30
console.log(deg(Math.asin(Math.sin((5 * Math.PI) / 6)))); // also 30
// atan2 keeps both quadrants apart
console.log(deg(Math.atan2(0.5, Math.cos(Math.PI / 6))));
console.log(deg(Math.atan2(0.5, Math.cos((5 * Math.PI) / 6))));
Discussion