How to create: Temperature converter

Learn how to use HTML and JavaScript to create a temperature converter.

Temperature converter

Type a value in any field to convert between temperature units:

Create a temperature converter

Create an input element that can convert one temperature unit to another.

Step 1 - Add HTML:

<p>
  <label>Fahrenheit</label>
  <input id="inputFahrenheit" type="number" placeholder="Fahrenheit"
  oninput="temperatureConverter(this.value)"
  onchange="temperatureConverter(this.value)">
</p>
<p>Celsius: <span id="outputCelsius"></span></p>

Step 2 - Add JavaScript:

Fahrenheit to Celsius conversion:

/* Convert Fahrenheit to Celsius when the input field receives input */
function temperatureConverter(valNum) {
  valNum = parseFloat(valNum);
  document.getElementById("outputCelsius").innerHTML = (valNum-32) / 1.8;
}

Try it yourself

Convert Fahrenheit to other units of measurement

The following table shows how to convert Fahrenheit to other temperature units:

Description Formula Example
Convert Fahrenheit to Celsius ℃=(℉-32)/1.8 Essayer
Convert Fahrenheit to Kelvin K=((℉-32)/1.8)+273.15 Essayer

Convert Celsius to other units of measurement

The following table shows how to convert Celsius to other temperature units:

Description Formula Example
Convert Celsius to Fahrenheit ℉=(℃*1.8)+32 Essayer
Convert Celsius to Kelvin K=℃+273.15 Essayer

Convert Kelvin to other units of measurement

The following table shows how to convert Kelvin to other temperature units:

Description Formula Example
Convert Kelvin to Fahrenheit ℉=((K-273.15)*1.8)+32 Essayer
Convertir Kelvin en degrés Celsius ℃=K-273.15 Essayer