JavaScript does not have a built-in Math.toRadians() method, so the usual solution is a small helper function. This is a common conversion when you work with geometry, canvas calculations, animation, or trigonometry.
The formula is simple: degrees multiplied by Math.PI / 180. If you later need the reverse direction, that is the same family of angle conversion problems and related math helpers.
Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.
Method 1: Convert degrees to radians with a helper function
The helper function multiplies the degree value by Math.PI / 180.
function degreesToRadians(degrees) {
return degrees * (Math.PI / 180);
}
console.log("to-radians:", degreesToRadians(180));You should see one line logging to-radians: 3.141592653589793.
Use this when you need a direct conversion for trigonometry or graphics math.
Method 2: Coterminal degrees in [0, 360) before converting
function degreesToRadiansNormalized(degrees) {
const d = ((degrees % 360) + 360) % 360;
return d * (Math.PI / 180);
}
console.log("to-radians-normalized-neg90:", degreesToRadiansNormalized(-90));You should see one line logging to-radians-normalized-neg90: 4.71238898038469.
That value is 3π/2 radians (equivalent to -90° for sin / cos). For many graphics APIs you instead keep the signed radians from Method 1; pick one convention and use it consistently.
Method 3: Use the conversion in trigonometric code
Most trigonometric functions expect radians, so the helper is the bridge between user-friendly degrees and computation.
const radians = 45 * (Math.PI / 180);
console.log("to-radians-cos:", Math.cos(radians).toFixed(2));You should see one line logging to-radians-cos: 0.71.
This is the version to reuse anywhere you need to convert degrees before calling a trig function.
Summary
JavaScript does not provide Math.toRadians(), so you usually build a small helper with degrees * (Math.PI / 180). Use Method 2’s double modulo when you need degrees wrapped to [0, 360) before converting; otherwise the direct product is enough for correct sin / cos / tan results on signed angles.
