Convert Fahrenheit to Celsius

Program to convert temperature from Fahrenheit to Celsius

BeginnerTopic: Basic Programs
Back

JavaScript Convert Fahrenheit to Celsius Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// 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`);
Output
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:

Subtract 32 from Fahrenheit
Multiply by 5/9 (or 0.5556)
Result is in Celsius

Common Temperatures:

Freezing: 32°F = 0°C
Boiling: 212°F = 100°C
Body temp: 98.6°F = 37°C
Room temp: 68°F = 20°C

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:

Reusable code
Clear purpose
Easy to test
Can be called multiple times

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.

Table of Contents