DocsHub
JavascriptBeginner

Temperature Converter

Build a temperature converter that converts between Celsius, Fahrenheit, and Kelvin in real time.

Temperature Converter

Problem

Build a temperature converter where the user types a value in any of the three fields — Celsius, Fahrenheit, or Kelvin — and the other two update instantly.

User types 100 in Celsius
Fahrenheit: 212
Kelvin: 373.15

User types 32 in Fahrenheit
Celsius: 0
Kelvin: 273.15

User types 0 in Kelvin
Celsius: -273.15
Fahrenheit: -459.67

User clears the input
Other fields: cleared too

Logic

  1. Select all three input fields
  2. Listen for the input event on each field
  3. When one field changes — read its value
  4. Convert to the other two units using the formulas
  5. Update the other two fields
  6. If the input is cleared — clear all fields

Formulas

Celsius to Fahrenheit  →  (C × 9/5) + 32
Celsius to Kelvin      →  C + 273.15

Fahrenheit to Celsius  →  (F − 32) × 5/9
Fahrenheit to Kelvin   →  (F − 32) × 5/9 + 273.15

Kelvin to Celsius      →  K − 273.15
Kelvin to Fahrenheit   →  (K − 273.15) × 9/5 + 32

Flow

yes no Celsius Fahrenheit Kelvin User types in any field input event fires Input is empty? Clear all fields Parse value as number Which field changed? Convert to F and K Convert to C and K Convert to C and F Update other two fields

HTML Structure

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Temperature Converter</title>
    <style>
      body {
        font-family: sans-serif;
        max-width: 400px;
        margin: 60px auto;
        padding: 0 20px;
      }

      h1 {
        margin-bottom: 24px;
      }

      /* each converter row */
      .field {
        display: flex;
        align-items: center;
        gap: 12px;
        margin-bottom: 16px;
      }

      .field label {
        width: 100px;
        font-weight: 500;
        font-size: 1rem;
      }

      .field input {
        flex: 1;
        padding: 10px;
        font-size: 1rem;
        border: 2px solid #ccc;
        border-radius: 8px;
        outline: none;
      }

      /* highlight the field being typed in */
      .field input:focus {
        border-color: #555;
      }

      .field span {
        font-size: 1.1rem;
        color: #555;
        width: 20px;
      }
    </style>
  </head>
  <body>
    <h1>Temperature Converter</h1>

    <!-- Celsius input -->
    <div class="field">
      <label for="celsius">Celsius</label>
      <input type="number" id="celsius" placeholder="°C" />
      <span>°C</span>
    </div>

    <!-- Fahrenheit input -->
    <div class="field">
      <label for="fahrenheit">Fahrenheit</label>
      <input type="number" id="fahrenheit" placeholder="°F" />
      <span>°F</span>
    </div>

    <!-- Kelvin input -->
    <div class="field">
      <label for="kelvin">Kelvin</label>
      <input type="number" id="kelvin" placeholder="K" />
      <span>K</span>
    </div>

    <script src="script.js"></script>
  </body>
</html>

Solution

// Step 1 — select all three input fields
const celsiusInput = document.querySelector("#celsius");
const fahrenheitInput = document.querySelector("#fahrenheit");
const kelvinInput = document.querySelector("#kelvin");

// Step 2 — helper to round to 2 decimal places
// avoids showing long floating point numbers like 212.00000000001
function round(value) {
  return Math.round(value * 100) / 100;
}

// Step 3 — attach input event to each field
// each listener only handles conversions from its own unit
celsiusInput.addEventListener("input", onCelsiusInput);
fahrenheitInput.addEventListener("input", onFahrenheitInput);
kelvinInput.addEventListener("input", onKelvinInput);

// Step 4 — Celsius handler
function onCelsiusInput() {
  const value = celsiusInput.value;

  // if field is cleared — clear the other two
  if (value === "") {
    fahrenheitInput.value = "";
    kelvinInput.value = "";
    return;
  }

  // parse string to number
  const celsius = parseFloat(value);

  // convert and update the other fields
  fahrenheitInput.value = round((celsius * 9) / 5 + 32);
  kelvinInput.value = round(celsius + 273.15);
}

// Step 5 — Fahrenheit handler
function onFahrenheitInput() {
  const value = fahrenheitInput.value;

  if (value === "") {
    celsiusInput.value = "";
    kelvinInput.value = "";
    return;
  }

  const fahrenheit = parseFloat(value);

  // convert Fahrenheit to Celsius first — then Celsius to Kelvin
  celsiusInput.value = round(((fahrenheit - 32) * 5) / 9);
  kelvinInput.value = round(((fahrenheit - 32) * 5) / 9 + 273.15);
}

// Step 6 — Kelvin handler
function onKelvinInput() {
  const value = kelvinInput.value;

  if (value === "") {
    celsiusInput.value = "";
    fahrenheitInput.value = "";
    return;
  }

  const kelvin = parseFloat(value);

  // convert Kelvin to Celsius first — then Celsius to Fahrenheit
  const celsius = kelvin - 273.15;
  celsiusInput.value = round(celsius);
  fahrenheitInput.value = round((celsius * 9) / 5 + 32);
}

Code Execution

Trace through typing 100 in Celsius:

StepCodeResult
Read valuecelsiusInput.value"100"
ParseparseFloat("100")100
To Fahrenheit(100 × 9/5) + 32212
To Kelvin100 + 273.15373.15
Update fieldsfahrenheit = 212, kelvin = 373.15done

Trace through typing 32 in Fahrenheit:

StepCodeResult
Read valuefahrenheitInput.value"32"
ParseparseFloat("32")32
To Celsius(32 - 32) × 5/90
To Kelvin0 + 273.15273.15
Update fieldscelsius = 0, kelvin = 273.15done

Trace through clearing the Celsius field:

StepCodeResult
Read valuecelsiusInput.value""
Check emptyvalue === ""true
Clear othersfahrenheit = "", kelvin = ""all cleared

Output

Type 100 in Celsius     → Fahrenheit: 212      Kelvin: 373.15
Type 32 in Fahrenheit   → Celsius: 0           Kelvin: 273.15
Type 0 in Kelvin        → Celsius: -273.15     Fahrenheit: -459.67
Clear any field         → All fields cleared

On this page