DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Loan Calculator App with JavaScript

A loan calculator is a perfect beginner project: it takes three numbers, runs one formula, and shows a result people actually care about. You will practice reading form inputs, doing real math in JavaScript, and updating the page without a reload. By the end you will have a tool your friends can genuinely use.

The math is the EMI formula — the same one banks use. Given a loan amount P, a monthly interest rate r, and the number of months n, the monthly payment is P × r × (1 + r)^n ÷ ((1 + r)^n − 1). In JavaScript that is a few lines.

function calculateEMI(principal, annualRate, years) {
  const r = annualRate / 12 / 100; // monthly rate
  const n = years * 12; // total months
  const emi = (principal * r * Math.pow(1 + r, n)) / (Math.pow(1 + r, n) - 1);
  return emi;
}

console.log(calculateEMI(500000, 9, 5)); // ~₹10,379 per month
Enter fullscreen mode Exit fullscreen mode

The page needs three inputs — loan amount, interest rate and tenure — plus a button and a results area. Read the values on submit, run the formula, and show the EMI, total interest and total payment, formatted as Indian rupees.

<input type="number" id="amount" placeholder="Loan amount (₹)">
<input type="number" id="rate" placeholder="Interest rate (%)">
<input type="number" id="years" placeholder="Tenure (years)">
<button id="calculate">Calculate EMI</button>
<p id="result"></p>
Enter fullscreen mode Exit fullscreen mode
document.querySelector("#calculate").addEventListener("click", () => {
  const amount = Number(document.querySelector("#amount").value);
  const rate = Number(document.querySelector("#rate").value);
  const years = Number(document.querySelector("#years").value);

  const emi = calculateEMI(amount, rate, years);
  const total = emi * years * 12;
  const interest = total - amount;

  document.querySelector("#result").textContent =
    `EMI: ₹${emi.toFixed(0)} | Total interest: ₹${interest.toFixed(0)} | Total payment: ₹${total.toFixed(0)}`;
});
Enter fullscreen mode Exit fullscreen mode

Two details make it feel professional: validate the inputs first — a zero rate or empty field should show a friendly message, not NaN — and format numbers with toLocaleString("en-IN") so 10379 reads as 10,379. A small chart or a slider for the amount is a nice next step once the basics work.

A calculator like this also teaches something bigger: how small changes in rate or tenure change what you pay. If you want to understand money better alongside the code, this guide to the best personal finance books for Indians is a good place to start: best personal finance books for Indians

Build it, test it with real numbers from a bank's website, and share it. A working calculator in your portfolio says more about your skills than a dozen tutorial certificates.

Top comments (0)