Open In App

JavaScript Program to Convert Radians to Degree

Radians and degrees are units of angular measurement used extensively in mathematics, physics, and engineering. Converting radians to degrees is a common operation in trigonometry and other mathematical calculations.

Example:

// Formula to convert radian into degree: 
degrees = radians × (180/π)
Where:
π is the mathematical constant equal to 3.14159.

Below are the approaches to convert radian to the degree in JavaScript:

Table of Content

Using Function

Here the code defines a function, convertRadiansToDegrees, that takes the radians as an input parameter for converting it into degrees. It then invokes the function with a radians value of π/4. Inside the function, use the formula: radians × (180/π), to convert it into degrees. Finally, return the result.

Example: The example below shows the demonstration of converting radians into degrees using a function.

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

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

Output
0.7853981633974483 radians is equal to 45 degrees.

Time Complexity: O(1)

Space Complexity: O(1)

Using Class

Here we will define a class named AngleConverter which will contain a method called convertToDegrees and will handle the conversion of radians to degrees. Instantiate the AngleConverter class using the new keyword, which creates an object of the class. Call the convertToDegrees method on the converter object, passing the angle in radians you want to convert as an argument. Print result.

Example: The example below shows the demonstration of converting radians into degrees using class.

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 is equal to ${degrees} degrees.`);

Output
0.7853981633974483 radians is equal to 45 degrees.

Time Complexity: O(1)

Space Complexity: O(1)

Article Tags :