JavaScript: Convert Celsius to Fahrenheit

1. Introduction

Temperature conversions are common tasks in scientific and daily life scenarios. In this guide, we'll dive into creating a JavaScript program that converts temperature from Celsius to Fahrenheit.

2. Program Overview

In this tutorial, we'll:

1. Declare a temperature in Celsius.

2. Implement a function to convert the Celsius temperature to Fahrenheit.

3. Display the result.

3. Code Program

let temperatureInCelsius = 25;  // Temperature in Celsius
let temperatureInFahrenheit;  // Variable to store the converted temperature

// Function to convert Celsius to Fahrenheit
function convertToFahrenheit(celsius) {
    return (celsius * 9/5) + 32;

temperatureInFahrenheit = convertToFahrenheit(temperatureInCelsius);

console.log(temperatureInCelsius + "°C is equal to " + temperatureInFahrenheit.toFixed(2) + "°F.");


25°C is equal to 77.00°F.

4. Step By Step Explanation

1. Variable Initialization: We start by defining our temperatureInCelsius which we aim to convert, and a variable temperatureInFahrenheit that will hold our result.

2. Conversion Function: The convertToFahrenheit(celsius) function carries out the conversion using the formula: (celsius × 9/5) + 32.

3. Performing the Conversion: We invoke the convertToFahrenheit function with our defined Celsius temperature, storing the outcome in temperatureInFahrenheit.

4. Displaying the Result: Using console.log, we then showcase the Celsius temperature and its equivalent Fahrenheit value. We utilize the toFixed(2) method to format the Fahrenheit temperature, ensuring it displays with two decimal places.