Convert Radians to Degrees in JavaScript



Radians and degrees are units of angular measurement used mainly in mathematics, physics, and engineering. Converting radians to degrees is important, as it is a common operation in trigonometry and other mathematical calculations. In this article, we will learn how to convert Radians to Degrees using JavaScript.

Radians to Degrees Converter

Result will appear here

Common Values

  • ? (3.14159)
  • ?/2 (1.5708)
  • ?/4 (0.7854)
  • ?/6 (0.5236)
  • 2? (6.28318)
  • 3?/2 (4.71239)

Formula to Convert Radian to Degree

degrees = radians × (180/?)

Example

Input 
const radians = Math.PI / 4

Output 
45

Input 
const radians = Math.PI / 2;

Output 
90

Approaches to Convert Radians to Degree

Using Function

We will write code that defines a function, convertRadiansToDegrees. This function will take radians as an input parameter and convert them into degrees. It then invokes the function with a radians value of ?/4. Inside the function, use the formula radians × (180/?) to convert radians into degrees. Finally, we will return the result.

Example Code

We will first define the function. Inside the function, we will write the formula to convert radians to degrees. and then we will print the result

function convertRadiansToDegrees(radians) {
    return radians * (180 / Math.PI);
}

const radians = Math.PI / 4;
const degrees = convertRadiansToDegrees(radians);
console.log(`${radians} radians are equal to ${degrees} degrees.`);

Output

0.7853981633974483 radians are equal to 45 degrees.

Complexity

  • Time Complexity: O(1), constant time
  • Space Complexity: O(1), constant space

Using Class

We can use a class that will contain a method called convertToDegrees and will handle the conversion of radians to degrees. We first Instantiate the AngleConverter class using the new keyword, which creates an object of the class. Then, we call the convertToDegrees method on the converter object, passing the angle in radians you want to convert as an argument. Finally, we print the result.

Example Code

We will first dfine the class. Then, we define the conversion method and write the conversion formula inside the method, i.e. degrees = radians × (180/?). Return the result. We Create an Object of the Class to use the AngleConverter class. Finally, print the result.

class AngleConverter {
    convertToDegrees(radians) {
        return radians * (180 / Math.PI);
    }
}

const converter = new AngleConverter();
const radians = Math.PI / 4;
const degrees = converter.convertToDegrees(radians);
console.log(`${radians} radians are equal to ${degrees} degrees.`);

Output

0.7853981633974483 radians are equal to 45 degrees.

Complexity

  • Time Complexity: O(1), constant time
  • Space Complexity: O(1), constant space
Updated on: 2024-11-15T12:18:14+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements