Open In App

JavaScript Program to Convert Farenheit to Celcius

Converting temperature from Fahrenheit to Celsius is a common task in programming, especially when dealing with weather data or scientific calculations. In this article, we’ll explore how to create a JavaScript program to perform this conversion using different approaches.

The formula to convert a temperature from Fahrenheit to Celsius is:



Celsius = (Fahrenheit - 32) * 5/9

Where:

Approach 1: Basic Function

A simple function can be used to perform the conversion. To convert Farenheit to Celcius, we will use math formula.






function fahrenheitToCelsius(fahrenheit) {
    return (fahrenheit - 32) * 5 / 9;
}
  
let fahrenheit = 100;
  
let celsius = fahrenheitToCelsius(fahrenheit);
console.log(`${fahrenheit}°F is ${celsius}°C`);

Output
100°F is 37.77777777777778°C

Approach 2: Using Arrow Function

An arrow function provides a more concise way to define the conversion function. In this case, use the arrow function to convert Farenheit to Celcius.




const fahrenheitToCelsius = fahrenheit => (fahrenheit - 32) * 5 / 9;
  
let fahrenheit = 100;
  
let celsius = fahrenheitToCelsius(fahrenheit);
console.log(`${fahrenheit}°F is ${celsius}°C`);

Output
100°F is 37.77777777777778°C
Article Tags :