Categories
JavaScript Answers

How to Get Trig Methods to Accept Degrees Instead of Radians in JavaScript?

Spread the love

To get the Math.sin, Math.cos, and Math.tan methods to use degrees instead of radians in JavaScript, we can convert the degree value accepted by Math.sin , Math.cos or Math.tan to radians.

For instance, we can write:

const toRadians = (angle) => {
  return angle * (Math.PI / 180);
}
console.log(Math.sin(toRadians(45)))
console.log(Math.cos(toRadians(45)))
console.log(Math.tan(toRadians(45)))

to create the toRadians function that converts the angle in degrees to radians by multiplying angle in degrees by Math.PI / 180 .

Then we call Math.sin , Math.cos , and Math.tan with toRadians(45) , which converts 45 degrees to radians.

And so we get:

0.7071067811865475
0.7071067811865476
0.9999999999999999

from the console log.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *