Emmanuel Akinfulubi

JavaScriptMath

Convert degrees to radians and use trig in JavaScript

Intermediate · 4 min read

Rotating a sprite in a game, drawing a clock face, working out the height of a ramp from its angle: all of these need trigonometry, the maths of angles and triangles. JavaScript has Math.sin, Math.cos and Math.tan built in. But there is a catch for beginners: they don't take degrees.

In this tutorial you will write functions to convert between degrees and radians, then try sine, cosine and tangent on the angles you met at school (30, 45, 60 and 90 degrees) and compare them with the exact values. Some come out a hair off, and Math.tan at 90 degrees doesn't give Infinity. You will learn why, and how to compare results safely. You need to know arrow functions and Math.PI.

Radians

Degrees split a full turn into 360. Radians measure a turn by the circle itself: a full turn is 2π radians, about 6.283. So:

  • 180 degrees (half a turn) is π radians.
  • 90 degrees is π / 2.
  • 30 degrees is π / 6.

To convert degrees to radians, multiply by π / 180. To go back, multiply by 180 / π.

The values from school

Sine, cosine and tangent take an angle and give a ratio of a right-angled triangle's sides. You may remember these exact values:

Output

sin 30° = 0.5     cos 60° = 0.5     tan 45° = 1
sin 90° = 1       cos 90° = 0       tan 90° = undefined

Let's see what JavaScript says.

Here is the exercise. Try it on your own first if you like, then read on and we will build it together.

80 Degrees, radians, and the trigonometry of known angles

fair

Convert between degrees and radians, then evaluate sine, cosine and tangent at 30, 45, 60 and 90 degrees and compare each with the exact value you learned. Find out what Math.sin(Math.PI) and Math.tan(Math.PI / 2) return.

Stuck? Three hints, smallest first
  1. 1. A full turn is 2 * Math.PI radians, so degrees times Math.PI / 180 gives radians.
  2. 2. Math.sin(rad(30)) is not 0.5, because rad(30) is not exactly a sixth of pi.
  3. 3. Math.tan(Math.PI / 2) has no reason to be Infinity: the argument is not exactly a quarter turn.

Step 1: the converters

Step 1

const rad = (deg) => (deg * Math.PI) / 180;
const deg = (r) => (r * 180) / Math.PI;
console.log(rad(180));                // 3.141592653589793
console.log(rad(30), Math.PI / 6);    // 0.5235987755982988 0.5235987755982988
console.log(rad(30) === Math.PI / 6); // true

rad(30) is exactly the same number as Math.PI / 6. The conversion itself is fine.

Step 2: the wrong turn, passing degrees

A common first try:

Step 2

console.log(Math.sin(30)); // -0.9880316240928618

That is the sine of 30 radians, nearly five full turns, not 30 degrees. JavaScript has no way to know you meant degrees, so it doesn't warn you. Always convert first.

Step 3: the special angles

Step 3

console.log(Math.sin(rad(30))); // 0.49999999999999994
console.log(Math.cos(rad(60))); // 0.5000000000000001
console.log(Math.tan(rad(45))); // 0.9999999999999999

None of them is exact. The reason starts with Math.PI. Pi has infinitely many digits, so Math.PI is only the closest number JavaScript can store. That means rad(30) is not exactly a sixth of the real pi, and the sine of a slightly-off angle is slightly off. Sine misses below 0.5, cosine misses above.

Step 4: round before you compare

For display, or for a comparison, round to the places you need:

Step 4

const round12 = (x) => Math.round(x * 1e12) / 1e12;
console.log([Math.sin(rad(30)), Math.cos(rad(60)), Math.tan(rad(45))].map(round12)); // [ 0.5, 0.5, 1 ]

Rounding to 12 decimal places gives back the values you learned. A test like Math.sin(rad(30)) === 0.5 is false, so round first, or check that the difference is smaller than a tolerance.

Step 5: 90 and 180 degrees

Step 5

console.log(Math.sin(rad(90)), Math.cos(rad(90))); // 1 6.123233995736766e-17
console.log(Math.sin(Math.PI));                    // 1.2246467991473532e-16

Math.sin(rad(90)) happens to be exactly 1. But the cosine of 90 degrees, which should be 0, is 6.1e-17, a tiny number just above zero. The sine of Math.PI should be 0 and is 1.2e-16. In both cases the angle is a hair away from the true one, and the result is a hair away from 0.

Step 6: tan at 90 degrees

The tangent of 90 degrees has no value in maths: the line goes straight up. So you might expect Infinity:

Step 6

console.log(Math.tan(Math.PI / 2));                  // 16331239353195370
console.log(Number.isFinite(Math.tan(Math.PI / 2))); // true

You get 16331239353195370: enormous, but finite. Math.PI / 2 is a tiny bit away from a real quarter turn, and the tangent of an angle near 90 degrees is huge but not infinite. So a check like === Infinity never catches it. Check the size instead, for example Math.abs(t) > 1e10.

Step 7: the round trip

Convert to radians and back again:

Step 7

const angles = [0, 30, 45, 60, 90, 180, 360];
console.log(angles.map((d) => deg(rad(d)))); // [ 0, 29.999999999999996, 45, 59.99999999999999, 90, 180, 360 ]

45, 90, 180 and 360 come back exactly. 30 and 60 come back a hair off. Each conversion rounds once, and for some angles the two roundings cancel out and for others they don't.

Some identities do hold well. sin² + cos² should always be 1, and at 37 degrees it is exactly 1:

JavaScript

console.log(Math.sin(rad(37)) ** 2 + Math.cos(rad(37)) ** 2); // 1

The full solution

The answer panel has rad, deg and round12, and prints 30 degrees in radians, the sine, cosine and tangent of the special angles raw and rounded, sin and cos of 90 degrees, Math.sin(Math.PI), Math.tan(Math.PI / 2) and the round trip for seven angles. Press Run to see the output.

The answer

const rad = (deg) => (deg * Math.PI) / 180;
const deg = (r) => (r * 180) / Math.PI;
const round12 = (x) => Math.round(x * 1e12) / 1e12;

const angles = [0, 30, 45, 60, 90, 180, 360];

console.log('30 degrees in radians:', rad(30), 'and Math.PI / 6 is', Math.PI / 6);
console.log('sin 30               :', Math.sin(rad(30)), '<- not 0.5');
console.log('cos 60               :', Math.cos(rad(60)), '<- not 0.5 either');
console.log('tan 45               :', Math.tan(rad(45)), '<- not 1');
console.log('rounded to 12 places :', [Math.sin(rad(30)), Math.cos(rad(60)), Math.tan(rad(45))].map(round12));
console.log('sin 90, cos 90       :', Math.sin(rad(90)), Math.cos(rad(90)));
console.log('sin PI               :', Math.sin(Math.PI));
console.log('tan PI / 2           :', Math.tan(Math.PI / 2), 'finite:', Number.isFinite(Math.tan(Math.PI / 2)));
console.log('round trip           :', angles.map((d) => deg(rad(d))));

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('30 degrees is exactly Math.PI / 6', rad(30) === Math.PI / 6, true);
test('but its sine is 0.49999999999999994, not 0.5', [Math.sin(rad(30)), Math.sin(rad(30)) === 0.5], [0.49999999999999994, false]);
test('cos 60 misses on the other side, and tan 45 is a bit under 1', [Math.cos(rad(60)), Math.tan(rad(45))], [0.5000000000000001, 0.9999999999999999]);
test('rounding to twelve places gives back the values you learned', [Math.sin(rad(30)), Math.cos(rad(60)), Math.tan(rad(45))].map(round12), [0.5, 0.5, 1]);
test('sin 90 is exactly 1, while cos 90 is 6.1e-17 rather than 0', [Math.sin(rad(90)), Math.cos(rad(90))], [1, 6.123233995736766e-17]);
test('sin of Math.PI is 1.2e-16, because Math.PI is not pi', Math.sin(Math.PI), 1.2246467991473532e-16);
test('tan of Math.PI / 2 is an enormous finite number, not Infinity', [Math.tan(Math.PI / 2), Number.isFinite(Math.tan(Math.PI / 2))], [16331239353195370, true]);
test('the round trip is exact at 45, 90, 180 and 360 but not at 30', angles.map((d) => deg(rad(d)) === d), [true, false, true, false, true, true, true]);
test('and sin squared plus cos squared is 1 at 37 degrees anyway', Math.sin(rad(37)) ** 2 + Math.cos(rad(37)) ** 2, 1);

Every trigonometric function in JavaScript takes radians, and Math.PI is a double, so the special angles are not special any more. rad(30) is the nearest double to a sixth of pi, its sine is 0.49999999999999994, and Math.cos(rad(60)) misses in the other direction at 0.5000000000000001. Anything comparing a trigonometric result to 0.5, 1 or 0 needs rounding or a tolerance. Note that rad(30) === Math.PI / 6 does hold, so the loss is in the sine rather than the conversion.

Math.tan(Math.PI / 2) is the one that surprises people: 16331239353195370, a perfectly finite number. The true tangent of a quarter turn is undefined, but Math.PI / 2 is a hair away from a quarter turn, and the tangent of a hair away is merely enormous. So there is no argument for which Math.tan returns Infinity, and code guarding a vertical asymptote with === Infinity never fires. Guard on magnitude instead. Same reason Math.sin(Math.PI) is 1.2e-16 rather than 0, and why the degree round trip loses 30 and 60 while keeping 45, 90 and 180.

Common mistakes

  • Passing degrees to Math.sin. Math.sin(30) is the sine of 30 radians. Convert with rad first.
  • Comparing trig results with ===. Math.sin(rad(30)) is not exactly 0.5. Round, or use a tolerance.
  • Checking for Infinity from Math.tan. It never returns Infinity for a real angle. Check the size of the result.

Try it yourself

  1. A ramp is 3 meters long and rises at 10 degrees. How high does it rise? (Height is length times the sine of the angle.)
  2. Write sinDeg(d) that takes degrees and returns the sine rounded to 12 places.
  3. Find the (x, y) position of the hour hand tip at 3 o'clock and at 2 o'clock on a clock with radius 100, using x = 100 * Math.cos(angle) and y = 100 * Math.sin(angle). What is the angle for each?

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.