Convert Fahrenheit to Celsius
Program to convert temperature from Fahrenheit to Celsius
JavaScript Convert Fahrenheit to Celsius Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Formula: Celsius = (Fahrenheit - 32) * 5/9
let fahrenheit = 98.6;
let celsius = (fahrenheit - 32) * 5 / 9;
console.log(`${fahrenheit}°F = ${celsius.toFixed(2)}°C`);
// Function version
function fahrenheitToCelsius(f) {
return (f - 32) * 5 / 9;
}
let temp1 = fahrenheitToCelsius(32); // Freezing point
let temp2 = fahrenheitToCelsius(212); // Boiling point
let temp3 = fahrenheitToCelsius(98.6); // Body temperature
console.log(`32°F = ${temp1}°C`);
console.log(`212°F = ${temp2}°C`);
console.log(`98.6°F = ${temp3.toFixed(2)}°C`);98.6°F = 37.00°C 32°F = 0°C 212°F = 100°C 98.6°F = 37.00°C
Understanding Convert Fahrenheit to Celsius
This program demonstrates temperature conversion and working with decimal numbers.
Temperature Conversion Formula
Celsius = (Fahrenheit - 32) × 5/9
Key Points:
Common Temperatures:
Working with Decimals
JavaScript handles floating-point arithmetic:
let result = (98.6 - 32) * 5 / 9;
// Result: 37.00000000000001 (floating point precision)Rounding Methods
1.
toFixed(n)
: Rounds to n decimal places, returns string
(37.00001).toFixed(2); // "37.00"
2.
Math.round()
: Rounds to nearest integer
Math.round(37.6); // 38
3.
Math.floor()
: Rounds down
Math.floor(37.9); // 37
4.
Math.ceil()
: Rounds up
Math.ceil(37.1); // 38
Function Approach
Creating a reusable function:
function fahrenheitToCelsius(f) {
}
return (f - 32) * 5 / 9;Benefits:
Reverse Conversion
Celsius to Fahrenheit:
function celsiusToFahrenheit(c) {
}
return (c * 9 / 5) + 32;Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Convert Fahrenheit to Celsius
This JavaScript program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.