To convert degrees to radians using JavaScript, you need to multiply the degrees by pi and divide by 180. Below is a function which will convert degrees to radians for you in JavaScript.
function degrees_to_radians(degrees) {
return degrees * (Math.PI / 180);
}
Converting degrees to radians involves the application of a very easy formula.
While JavaScript doesn’t have a function which will convert degrees to radians for us, we can easily define a function. To convert degrees to radians, all we need to do is multiply the degrees by pi divided by 180.
We can convert degrees to radians without the help of the math module easily in Python.
Below is a user-defined function which will convert degrees to radians for us in our Python code.
function degrees_to_radians(degrees) {
return degrees * (Math.PI / 180);
}
Below are some examples of how to convert a number in degrees to radians using JavaScript.
function degrees_to_radians(degrees) {
return degrees * (Math.PI / 180);
}
console.log(degrees_to_radians(0))
console.log(degrees_to_radians(30))
console.log(degrees_to_radians(60))
console.log(degrees_to_radians(90))
// Output:
0.0
0.5235987755982988
1.0471975511965976
1.5707963267948966
Converting Radians to Degrees in JavaScript
If you want to go the other way, from radians to degrees, you have to define another function in JavaScript.
Luckily, the formula is just a reverse of the degrees to radians conversion formula. To convert radians to degrees, we multiply by 180 and divide by pi.
Below are some examples of how to convert a number in radians to degrees using JavaScript.
function radians_to_degrees(radians) {
return radians * (180 / Math.PI);
}
console.log(radians_to_degrees(0))
console.log(radians_to_degrees(Math.PI/6))
console.log(radians_to_degrees(Math.PI/3))
console.log(radians_to_degrees(Math.PI/2))
// Output:
0
29.999999999999996
59.99999999999999
90
Hopefully this article has been useful for you to learn how to convert degrees to radians in your JavaScript code.