Loading your tools...
Loading your tools...
Learn to build a Mortgage Calculator. We break down the scary amortization formula into simple JavaScript steps.
Hello Future Homeowners & Coders! 🏠💻
Mortgages are confusing. You borrow $300,000, but over 30 years, you might pay back $600,000!
This is called The Pizza Paradox: Imagine buying a pizza for $20, but because you eat it slowly over 30 years, the shop charges you $40.
Today, we will build a tool to calculate exactly how much that "Pizza" (House) really costs. We'll tame the Scary Mortgage Formula using simple JavaScript.
We need 3 main numbers to calculate a loan:
<div class="calculator-card">
<h2>🏡 Mortgage Calculator</h2>
<label>Loan Amount ($)</label>
<input type="number" id="principal" placeholder="300000">
<label>Interest Rate (%)</label>
<input type="number" id="rate" placeholder="5.5">
<label>Loan Term (Years)</label>
<input type="number" id="years" placeholder="30">
<button onclick="calculateMortgage()">Calculate Payment</button>
<div id="result">Monthly Payment: $0.00</div>
</div>
Let's grab our values.
function calculateMortgage() {
// 1. Get the raw numbers
var P = Number(document.getElementById('principal').value);
var annualRate = Number(document.getElementById('rate').value);
var years = Number(document.getElementById('years').value);
if(P === 0 || annualRate === 0 || years === 0) {
alert("Please fill in all fields!");
return;
}
}
The formula needs the Monthly Interest Rate as a decimal, not the Annual Percentage.
// 2. Convert Annual Rate to Monthly Decimal
// Example: 5% -> 0.05 -> 0.00416...
var r = annualRate / 1200;
The formula counts Payments, not Years.
// 3. Convert Years to Months (Total Payments)
var n = years * 12;
Formula Part 1: r * (1 + r)^n
We use Math.pow(base, exponent) for the power.
// 4. Calculate Top of Fraction
var numerator = r * Math.pow(1 + r, n);
Formula Part 2: (1 + r)^n - 1
// 5. Calculate Bottom of Fraction
var denominator = Math.pow(1 + r, n) - 1;
Formula Part 3: P * (Top / Bottom)
// 6. Divide and Multiply by Principal
var monthlyPayment = P * (numerator / denominator);
325.1299999 looks bad. We want $325.13.
We use .toFixed(2) for this.
// 7. Format Result
var resultText = monthlyPayment.toFixed(2);
document.getElementById('result').innerText = "Monthly Payment: $" + resultText;
Financial tools should look clean and professional. Let's use green for money!
<style>
.calculator-card {
background: white; padding: 30px;
border-radius: 10px; width: 350px;
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
font-family: Arial, sans-serif;
}
input {
width: 100%; padding: 10px; margin: 5px 0 15px 0;
border: 1px solid #ddd; border-radius: 5px;
box-sizing: border-box; /* Keeps padding inside width */
}
button {
width: 100%; padding: 12px; background: #2ecc71;
color: white; border: none; border-radius: 5px;
font-size: 16px; cursor: pointer;
}
button:hover { background: #27ae60; }
#result {
margin-top: 20px; font-size: 22px;
color: #2c3e50; font-weight: bold; text-align: center;
}
</style>
Here is the complete tool. Copy-paste this and you're ready to analyze loans!
<!DOCTYPE html>
<html>
<head>
<title>Simple Mortgage Calculator</title>
<style>
body { background: #ecf0f1; display: flex; justify-content: center; align-items: center; height: 100vh; font-family: 'Helvetica Neue', sans-serif; margin: 0;}
.card { background: white; padding: 40px; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); width: 320px; }
h2 { text-align: center; color: #34495e; margin-top: 0; }
label { font-size: 13px; color: #7f8c8d; font-weight: bold; display: block; margin-top: 15px; }
input {
width: 100%; padding: 12px; margin-top: 5px;
border: 2px solid #ecf0f1; border-radius: 8px;
font-size: 16px; transition: 0.3s;
box-sizing: border-box;
}
input:focus { border-color: #3498db; outline: none; }
button {
width: 100%; padding: 15px; margin-top: 25px;
background: #27ae60; color: white;
border: none; border-radius: 8px;
font-size: 16px; font-weight: bold; cursor: pointer;
transition: 0.3s;
}
button:hover { background: #219150; transform: translateY(-2px); }
#output-box {
margin-top: 25px; padding: 20px;
background: #f8f9fa; border-radius: 8px;
text-align: center;
}
.small-text { font-size: 12px; color: #95a5a6; text-transform: uppercase; letter-spacing: 1px;}
.big-money { font-size: 32px; color: #2c3e50; font-weight: 800; margin: 5px 0; }
</style>
</head>
<body>
<div class="card">
<h2>🏡 Mortgage Calc</h2>
<label>LOAN AMOUNT ($)</label>
<input type="number" id="p" placeholder="300000">
<label>INTEREST RATE (%)</label>
<input type="number" id="r" placeholder="5.0">
<label>LOAN TERM (YEARS)</label>
<input type="number" id="y" placeholder="30">
<button onclick="calc()">Calculate Payment</button>
<div id="output-box">
<div class="small-text">Estimated Monthly Payment</div>
<div class="big-money" id="res">$0.00</div>
</div>
</div>
<script>
function calc() {
// 1. Get Inputs
let P = parseFloat(document.getElementById('p').value);
let ratePercent = parseFloat(document.getElementById('r').value);
let years = parseFloat(document.getElementById('y').value);
// Validation
if(!P || !ratePercent || !years) {
alert("Please enter valid numbers for all fields");
return;
}
// 2. Convert Rate (Annual % -> Monthly Decimal)
let monthlyRate = ratePercent / 100 / 12;
// 3. Convert Years to Total Months
let totalPayments = years * 12;
// 4. The Formula: M = P[r(1+r)^n]/[(1+r)^n-1]
// Calculate (1+r)^n first (it's used twice)
let compoundFactor = Math.pow(1 + monthlyRate, totalPayments);
// Calculate Numerator & Denominator
let numerator = monthlyRate * compoundFactor;
let denominator = compoundFactor - 1;
// Final Division
let monthlyPayment = P * (numerator / denominator);
// 5. Display Result
document.getElementById('res').innerText = "$" + monthlyPayment.toFixed(2);
}
</script>
</body>
</html>
You've successfully built a financial tool that helps people make the biggest purchase of their lives!
Remember: This same logic applies to Auto Loans and Personal Loans too. The only difference is usually the term (5 years for a car vs 30 for a house).
Try Pro Mortgage Calculator »Developer Tools & Resource Experts
FastTools is dedicated to curating high-quality content and resources that empower developers. With nearly 5 years of hands-on development experience, our team rigorously evaluates every tool and API we recommend, ensuring you get only the most reliable and effective solutions for your projects.