EN
JavaScript - convert radians to degrees (rad to deg)
12
points
In this article, we're going to have a look at how to convert radians to degrees using JavaScript. In JavaScript, there is no built-in conversion method. It is necessary to write own one.
Conversion can be made with the following formula:
A simple implementation is presented in the below.
Custom conversion method example
// ONLINE-RUNNER:browser;
function radToDeg(rad) {
return rad * (180.0 / Math.PI);
}
// Usage example:
console.log( radToDeg( 0 ) ); // 0.0 degrees
console.log( radToDeg( Math.PI / 2 ) ); // 90.0 degrees
console.log( radToDeg( Math.PI ) ); // 180.0 degrees
console.log( radToDeg( 3.141592653589793 / 6 ) ); // ~30 degrees
console.log( radToDeg( 3.141592653589793 / 4 ) ); // 45 degrees
console.log( radToDeg( 3.141592653589793 / 2 ) ); // 90 degrees
console.log( radToDeg( 3.141592653589793 ) ); // 180 degrees
console.log( radToDeg( 3.141592653589793 * 1.5 ) ); // 270 degrees
console.log( radToDeg( 3.141592653589793 * 2 ) ); // 360 degrees