Open In App

JavaScript Program to Convert Degree to Radian

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

Degrees and Radians

// Formula to convert degree into radian: 

radians = degrees × (π/180)

Table of Content

Using Function

In this function-based approach, a function named 'convertDegreesToRadians' is defined to convert degrees to radians. It takes a degree value as input, applies the conversion formula (degrees × (π/180)), and returns the result. This approach offers a concise and reusable way to perform degree-to-radian conversions in JavaScript.

Example: The example below shows the demonstration of converting degree into radian using a function.

function convertDegreesToRadians(degrees) {
    return degrees * (Math.PI / 180);
}

const degrees = 45; 
const radians = 
    convertDegreesToRadians(degrees);
console.log(`${degrees} degrees is equal to ${radians} radians.`);

Output
45 degrees is equal to 0.7853981633974483 radians.

Time Complexity: O(1)

Space Complexity: O(1)

Using Class

In this class-based approach, an AngleConverter class is defined with a method convertToRadians to handle the conversion of degrees to radians. An instance of the AngleConverter class is created using the new keyword, and the convertToRadians method is called on the converter object, passing the angle in degrees as an argument, yielding the result.

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

class AngleConverter {
    convertToRadians(degrees) {
        return degrees * (Math.PI / 180);
    }
}

const converter = new AngleConverter();
const degrees = 45; 
const radians = converter.convertToRadians(degrees);
console.log(`${degrees} degrees is equal to ${radians} radians.`);

Output
45 degrees is equal to 0.7853981633974483 radians.

Time Complexity: O(1)

Space Complexity: O(1)

Article Tags :