w3resource

JavaScript: Convert radians to degrees

JavaScript Math: Exercise-34 with Solution

Write a JavaScript function to convert radians to degrees.

Test Data : console.log(radians_to_degrees(0.7853981633974483));
45

Sample Solution-1:

JavaScript Code:

function radians_to_degrees(radians)
{
  var pi = Math.PI;
  return radians * (180/pi);
}
          
console.log(radians_to_degrees(0.7853981633974483));

Sample Output:

45

Pictorial Presentation:

JavaScript: Math - Convert radians to degrees.

Flowchart:

Flowchart: JavaScript Math: Convert radians to degrees

Sample Solution-2:

JavaScript Code:

const radians_to_degrees = rad => (rad * 180.0) / Math.PI;
console.log(radians_to_degrees(Math.PI / 2));
console.log(radians_to_degrees(0.7853981633974483));

Sample Output:

90
45

Live Demo:

See the Pen javascript-math-exercise-34 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript function to convert an angle from degrees to radians.
Next: Write a JavaScript function for the Pythagorean theorem.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

data storage

sessionStorage.setItem('cool_secret', 123);

The data stored in sessionStorage is removed after closing the tab.
If you used localStorage, the data would've been there forever, unless for example localStorage.clear() is invoked.

Ref: https://bit.ly/323Y0P6