Convert Fahrenheit to Celsius in JavaScript
Convert Fahrenheit to Celsius in JavaScript with C = (F - 32) * 5 / 9. Includes a reusable function and toFixed rounding, with explanation and output.
1// Formula: Celsius = (Fahrenheit - 32) * 5/92let fahrenheit = 98.6;3let celsius = (fahrenheit - 32) * 5 / 9;45console.log(`${fahrenheit}°F = ${celsius.toFixed(2)}°C`);67// Function version8function fahrenheitToCelsius(f) {9 return (f - 32) * 5 / 9;10}1112let temp1 = fahrenheitToCelsius(32); // Freezing point13let temp2 = fahrenheitToCelsius(212); // Boiling point14let temp3 = fahrenheitToCelsius(98.6); // Body temperature1516console.log(`32°F = ${temp1}°C`);17console.log(`212°F = ${temp2}°C`);18console.log(`98.6°F = ${temp3.toFixed(2)}°C`);
Sample Output
98.6°F = 37.00°C 32°F = 0°C 212°F = 100°C 98.6°F = 37.00°C
In this one
- How to turn a math formula into code
- Why parentheses control the order of work
- How toFixed() rounds a number for display
- How to test a formula with known values
How it works
- 1
fahrenheit is set to 98.6.
- 2
(98.6 - 32) * 5 / 9 gives 37.
- 3
toFixed(2) prints 37.00.
- 4
The function is called with 32, 212 and 98.6.
- 5
Each result is printed with °F and °C labels.
Edge cases
Missing parentheses
f - 32 * 5 / 9 gives the wrong answer.
* and / run before -, so the 32 is multiplied first. Write (f - 32).
Negative temperatures
-40°F converts to -40°C.
The formula works for negatives. -40 is the one point where both scales match, a good extra test.
Input from a form
A string like "98.6" still works with - but not with +.
Convert with Number() first so every operator behaves the same way.
What's going on
This program converts a temperature from Fahrenheit to Celsius. The formula is C = (F - 32) × 5 / 9. Many countries use Celsius, while weather apps from the US often use Fahrenheit, so this conversion is a common small task.
First the program converts 98.6°F, which is normal body temperature. It subtracts 32, multiplies by 5 and divides by 9. The result is 37. toFixed(2) formats it with two decimal places, so it prints 37.00. toFixed returns a string, which is fine for display.
The second part wraps the formula in a function called fahrenheitToCelsius. A function is a named block of code you can call again with different inputs. The program calls it three times: for 32, 212 and 98.6.
Use known values to test your formula. 32°F must give 0°C, the freezing point of water. 212°F must give 100°C, the boiling point. If 32 gives 17.7 instead of 0, you forgot to subtract 32 before multiplying.
The parentheses around (f - 32) matter. Without them, JavaScript would multiply 32 × 5 / 9 first and subtract that from f. Always group the subtraction so the formula matches the math.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What should 212°F convert to?
Pick an answer to check it.
Question 2
What does (98.6).toFixed(1) return?
Pick an answer to check it.
Questions people ask
Why does 32°F print 0 without decimals?
The function result is exactly 0, and that line does not call toFixed.
How do I convert Celsius back to Fahrenheit?
Use F = C * 9 / 5 + 32.
Does toFixed change the variable?
No. It returns a new string and leaves the number as it was.