w3resource

JavaScript: Convert an angle from degrees to radians

JavaScript Math: Exercise-33 with Solution

Write a JavaScript function to convert an angle from degrees to radians.

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

Use Math.PI and the degree to radian formula to convert the angle from degrees to radians.

Sample Solution-1:

JavaScript Code:

function degrees_to_radians(degrees)
{
  var pi = Math.PI;
  return degrees * (pi/180);
}

console.log(degrees_to_radians(45));

Sample Output:

0.7853981633974483

Pictorial Presentation:

JavaScript: Math - Convert degrees to radians.

Flowchart:

Flowchart: JavaScript Math- Convert degrees to radians

Sample Solution-2:

JavaScript Code:

const degrees_to_radians = deg => (deg * Math.PI) / 180.0;
console.log(degrees_to_radians(45));
console.log(degrees_to_radians(90));

Sample Output:

0.7853981633974483
1.5707963267948966

Live Demo:

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


Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript function to calculate the percentage (%) of a number.
Next: Write a JavaScript function to convert radians to degrees.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.