To convert Celsius to Fahrenheit in JavaScript, use the formula F = C * 9/5 + 32. In code that looks like const fahrenheit = celsius * 9 / 5 + 32;. That single line is the whole answer; the rest of this guide covers writing it as a reusable function, handling user input, avoiding the usual bugs, and testing it so the numbers come out right every time.
What You'll Need
- A text editor (VS Code, Notepad++, or any plain editor).
- A JavaScript runtime: a browser's developer console, Node.js, or an online sandbox such as the one built into your browser's DevTools.
- Basic familiarity with variables and functions. No libraries or packages are required.
Step-by-Step
- Write the raw formula first. Open your editor and create a file called
temp.js. Add:const celsius = 100;
const fahrenheit = celsius * 9 / 5 + 32;
console.log(fahrenheit); // 212
Run it withnode temp.jsor paste it into the browser console. If you see 212, the math is correct. - Wrap it in a function. A reusable function keeps the formula in one place:
function celsiusToFahrenheit(celsius) {
return celsius * 9 / 5 + 32;
}
Call it withcelsiusToFahrenheit(0)and you should get 32. - Decide how to round. Floating-point math can produce values like 98.60000000000001. Use
toFixed(1)for one decimal place orMath.round()for a whole number. Example:return (celsius * 9 / 5 + 32).toFixed(1);returns a string, so convert back withNumber()if you need to do more math. - Validate the input. If the value comes from a form field, it arrives as a string. Convert it with
Number(celsius)and check withNumber.isFinite()before calculating. Reject empty strings and non-numeric text with a clear message. - Handle the two special cases. Water freezes at 0 °C (32 °F) and boils at 100 °C (212 °F) at standard pressure. Use these as quick sanity checks after any change to the function.
- Wire it to a simple page (optional). Add an
<input type="number">, a button, and a paragraph for output. On click, readinput.value, run the function, and write the result withtextContent. AvoidinnerHTMLfor user-supplied values. - Test edge values. Run -40 (both scales meet at -40), 0, 37 (body temperature, about 98.6 °F), and 100. If any result is off by a consistent amount, check operator precedence: multiplication and division happen before addition, so
c * 9 / 5 + 32is correct without extra parentheses.
Common Mistakes
Using the wrong fraction. The multiplier is 9/5 (or 1.8), not 5/9. Reversing it gives nonsense for most values.
Forgetting the +32. Multiplying alone converts to a scaled number, not Fahrenheit. The offset is what shifts the zero point.
Concatenating strings. If celsius is the string "20", then celsius * 9 / 5 + 32 happens to work because * coerces, but celsius + 32 would produce "2032". Always convert input with Number() first.
Rounding too early. Round only the final result. Rounding intermediate steps accumulates error.
Returning a string by accident. toFixed() returns a string. If you chain more math onto it, you may get unexpected results; wrap it in Number().
Tips
Name the function after the direction, celsiusToFahrenheit, so the reverse function fahrenheitToCelsius is obvious. The reverse formula is (f - 32) * 5 / 9.
Keep the constant 9 / 5 as written rather than 1.8 if you want the code to mirror the formula readers already know; both are correct.
Add a short comment above the function showing one worked example, such as // 100 C = 212 F. It saves the next reader a mental check.
For display, include the unit in the output string, for example result + " °F", so the number is never ambiguous.
Troubleshooting
Output is NaN. The input was not a number. Log the raw value before conversion and confirm it is not undefined, an empty string, or text with a comma decimal separator.
Output is a long decimal. That is normal floating-point behaviour. Apply toFixed() or Math.round() at the end.
Output is wildly wrong. Check that you used 9/5 and not 5/9, and that +32 is present. Test with 0 °C: the answer must be 32.
The page shows nothing. Confirm the script runs after the elements exist, either by placing the script at the end of the body or by listening for DOMContentLoaded. Check the browser console for errors.
Negative values look off. They should not. -40 °C is exactly -40 °F; if you get a different number, the formula has been altered.
Frequently Asked Questions
What is the exact formula?
F = C × 9/5 + 32. In JavaScript: c * 9 / 5 + 32.
Can I use 1.8 instead of 9/5?
Yes. 9/5 equals 1.8, so c * 1.8 + 32 gives the same result.
How do I convert back from Fahrenheit to Celsius?
Use (f - 32) * 5 / 9. Subtract the offset first, then scale.
Why does my result have many decimal places?
Binary floating-point cannot represent every decimal exactly. Round the final value with toFixed() or Math.round().
Should the function return a number or a string?
Return a number for reuse in calculations; format it into a string only when displaying it.
Does this work in Node.js and the browser?
Yes. The formula is plain JavaScript and needs no browser-specific features.