JavaScript Math.toRadians | Convert Degrees to Radians


Olorunfemi Akinlua

JavaScript

Introduction

In JavaScript, we have a Math object that provides tons of methods to achieve certain mathematical operations. Unlike Java, there is no math.toradians method in JavaScript.

The Math.toRadians method is supposed to convert an angle from degrees to radians.

Radians and degrees are both units of angle measurement, but they represent the angle in different ways. In radians, the angle is measured in terms of the radius of a circle, with 2π radians equal to a full circle. In degrees, the angle is measured in terms of a full circle, with 360 degrees equal to a full circle.

In this article, we will create our custom toradians function in JavaScript.

 

Convert degrees to radians using Math.toRadians

The formula to convert degrees to radians can be seen below

radians = degrees x (pi / 180)

To create that in JavaScript, we can make use of the Math.PI property value and obtain the degree value from the user.

Here is the way to write a function that converts degrees to radians in JavaScript:

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

To use this function, you would call it like this:

console.log(degreesToRadians(180))

Output

3.141592653589793

This function converts degrees to radians by multiplying the number of degrees by π / 180. This is a mathematical conversion factor that is used to convert between the two units.

Note that this function will only work for positive degree values. If you want to be able to convert negative degrees to radians as well, you can modify the function like this:

function degreesToRadians(degrees) {
    return (degrees % 360) * (Math.PI / 180);
}

This version of the function takes the remainder of the degree value when divided by 360, which allows it to handle negative degrees. For example:

console.log(degreesToRadians(-90));

Output

-1.5707963267948966

 

Summary

JavaScript comes with a lot of native methods but unfortunately, it doesn’t have the math.toradians method to help convert from degrees to radians. We have shown how we can make use of the Math object property to make a custom toradians method.

 

References

Math.PI - JavaScript | MDN (mozilla.org)

 

Views: 31

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on LinkedIn.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

GoLinuxCloud Logo


We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages

JavaScript

Python

Golang

Node.js

Java

Laravel