How to convert Celsius to Fahrenheit in JavaScript
The two most often used temperature scales are as follows:
- Fahrenheit
- Celsius
Celsius vs. Fahrenheit
Celsius is the world’s most widely used temperature scale, commonly known as Centigrade and written as °C. Celsius is most widely used in scientific and medical applications because it is simpler to work with. Fahrenheit (written as °F) is still commonly used in many everyday applications in the United States.
Celsius to Fahrenheit conversion formula
We can convert the temperature from Celsius to Fahrenheit using the formula below.
where, is the Fahrenheit temperature, and is the Celsius temperature.
Problem statement
We are given a temperature value and need to convert that temperature to Fahrenheit using the above formula.
Explanation
Let’s suppose we have a temperature of degrees Celsius. Putting the value of in the above formula,
Given, .
Implementation
Let’s see an example of converting the temperature of Celsius to Fahrenheit in JavaScript:
let c = 50;let f = 0;// Using the above formulaf = (c * (9 / 5)) + 32;console.log("The value of the temperature in Fahrenheit is " + f);
Code explanation
- Lines 1–2: We initialize variables
candfwith values50and0, respectively. - Line 5: We use the formula to calculate the value of
f. - Line 6: We print the value of
fto the console.
Free Resources