DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Currency Converter App with JavaScript

A currency converter is one of the cleanest beginner projects in JavaScript: a small form, one API call, and a result. In this tutorial we will build a converter that turns an amount in one currency into another, shows the exchange rate, and remembers your last conversion. It teaches fetching, form handling, and caching — skills you will use in every app you build.

First, the data source. Free exchange-rate APIs return a base currency and a table of rates. We fetch the rates once, cache them, and do the math locally. That keeps the app fast and avoids calling the API on every keystroke.

// Fetch exchange rates once, then convert locally
let ratesCache = null;

async function getRates(base = "USD") {
  if (ratesCache) return ratesCache;
  const res = await fetch(`https://api.exchangerate-api.com/v4/latest/${base}`);
  if (!res.ok) throw new Error("Could not load exchange rates");
  const data = await res.json();
  ratesCache = data.rates;
  return ratesCache;
}

async function convert(amount, from, to) {
  const rates = await getRates("USD");
  const inUsd = amount / rates[from];
  return inUsd * rates[to];
}
Enter fullscreen mode Exit fullscreen mode

Next, the interface. Two dropdowns for the currencies, an input for the amount, and a convert button. On submit, we show the result and the rate used — for example, "100 USD = 8,350 INR (1 USD = 83.50 INR)". A swap button exchanges the two currencies, which is the one feature every user tries first, so make sure it works.

document.getElementById("convert-form").addEventListener("submit", async (e) => {
  e.preventDefault();
  const amount = parseFloat(document.getElementById("amount").value);
  const from = document.getElementById("from").value;
  const to = document.getElementById("to").value;
  try {
    const result = await convert(amount, from, to);
    document.getElementById("result").textContent =
      `${amount} ${from} = ${result.toFixed(2)} ${to}`;
  } catch (err) {
    document.getElementById("result").textContent =
      "Could not convert right now — check your connection and try again.";
  }
});
Enter fullscreen mode Exit fullscreen mode

Converting money is also a small lesson in personal finance: knowing what your money is worth in another currency is useful when you travel, shop online, or send money abroad. If you want to understand money better — saving, budgeting, and how currencies work — a good next step is reading a few personal finance books, which explain these ideas in simple, practical language.

To go further, add a favorites row for the currencies you use most, a history list of recent conversions saved in localStorage, and a small chart of the rate over the last 30 days. Each is a small feature, and together they turn a toy into a tool you will actually use.

This project teaches the core skills of every API-driven app: fetching once and caching, converting data locally, and handling errors without crashing. Build it in an afternoon, and you will have a tool you can genuinely use on your next trip.

Top comments (1)

Collapse
 
alexshev profile image
Alex Shev •

Currency conversion looks simple until the rate becomes a financial input rather than a display detail. I’d show the rate source, timestamp, base currency, rounding policy, and fallback behavior when the provider is unavailable, then test a few inverse and cross-rate cases. That makes the app’s result explainable instead of giving users a precise-looking number with unknown assumptions.