How to Convert Degrees to Radians in JavaScript

Contents
In this article, you will learn how to convert degrees to radians in JavaScript.
Converting degrees to radians in JavaScript
Radians and degrees are two ways to measure angles. Radians are a more mathematically convenient way to measure angles, but degrees are more commonly used in everyday life. In JavaScript, we can convert degrees to radians using a simple formula.
The formula to convert degrees to radians is:
radians = degrees * (Math.PI / 180)
where degrees is the angle measurement in degrees and Math.PI is the mathematical constant for pi, which is approximately equal to 3.14159.
Examples
Convert 45 degrees to radians
const degrees = 45;
const radians = degrees * (Math.PI / 180);
console.log(radians); // 0.7853981633974483
In this example, we first define the angle measurement degrees to be 45. We then use the formula to convert degrees to radians and store the result in the variable radians. The output is the value of radians, which is approximately equal to 0.7854 radians.
Convert 90 degrees to radians
const degrees = 90;
const radians = degrees * (Math.PI / 180);
console.log(radians); // 1.5707963267948966
In this example, we define the angle measurement degrees to be 90. We then use the formula to convert degrees to radians and store the result in the variable radians. The output is the value of radians, which is approximately equal to 1.5708 radians.
Convert 180 degrees to radians
const degrees = 180;
const radians = degrees * (Math.PI / 180);
console.log(radians); // 3.141592653589793
In this example, we define the angle measurement degrees to be 180. We then use the formula to convert degrees to radians and store the result in the variable radians. The output is the value of radians, which is approximately equal to 3.1416 radians.