Convert Fahrenheit to Celsius in JavaScript

C = (F - 32) * 5/9. 32°F should be 0. If you get 17.7 you forgot the minus 32.

BeginnerBasic ProgramsExample 8 of 10
convert-fahrenheit-to-celsius.js
1// Formula: Celsius = (Fahrenheit - 32) * 5/9
2let fahrenheit = 98.6;
3let celsius = (fahrenheit - 32) * 5 / 9;
4
5console.log(`${fahrenheit}°F = ${celsius.toFixed(2)}°C`);
6
7// Function version
8function fahrenheitToCelsius(f) {
9 return (f - 32) * 5 / 9;
10}
11
12let temp1 = fahrenheitToCelsius(32); // Freezing point
13let temp2 = fahrenheitToCelsius(212); // Boiling point
14let temp3 = fahrenheitToCelsius(98.6); // Body temperature
15
16console.log(`32°F = ${temp1}°C`);
17console.log(`212°F = ${temp2}°C`);
18console.log(`98.6°F = ${temp3.toFixed(2)}°C`);

Output

98.6°F = 37.00°C
32°F = 0°C
212°F = 100°C
98.6°F = 37.00°C

What's going on

(f - 32) * 5/9. Parentheses matter in your head even when precedence saves you.

32 → 0, 212 → 100. Those are the sanity checks.