function calculateMortgage() {
// 1. Get Input Values using var
var homePrice = parseFloat(document.getElementById("homePrice").value);
var downPayment = parseFloat(document.getElementById("downPayment").value);
var loanTermYears = parseInt(document.getElementById("loanTerm").value);
var interestRateAnnual = parseFloat(document.getElementById("interestRate").value);
// 2. Validate Inputs
if (isNaN(homePrice) || homePrice <= 0) {
alert("Please enter a valid Home Price.");
return;
}
if (isNaN(downPayment) || downPayment < 0) {
alert("Please enter a valid Down Payment.");
return;
}
if (isNaN(interestRateAnnual) || interestRateAnnual < 0) {
alert("Please enter a valid Interest Rate.");
return;
}
// 3. Logic: Calculate Loan Principal
var principal = homePrice – downPayment;
if (principal <= 0) {
alert("Down payment cannot be equal to or greater than the home price for a mortgage.");
return;
}
// 4. Logic: Rate and Term conversions
var monthlyRate = (interestRateAnnual / 100) / 12;
var numberOfPayments = loanTermYears * 12;
// 5. Logic: Amortization Formula
// M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
var monthlyPayment = 0;
if (interestRateAnnual === 0) {
monthlyPayment = principal / numberOfPayments;
} else {
var numerator = monthlyRate * Math.pow((1 + monthlyRate), numberOfPayments);
var denominator = Math.pow((1 + monthlyRate), numberOfPayments) – 1;
monthlyPayment = principal * (numerator / denominator);
}
// 6. Logic: Total Costs
var totalCost = monthlyPayment * numberOfPayments;
var totalInterest = totalCost – principal;
// 7. Format Output Currency
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2
});
// 8. Update DOM
document.getElementById("monthlyPaymentDisplay").innerText = formatter.format(monthlyPayment);
document.getElementById("loanAmountDisplay").innerText = formatter.format(principal);
document.getElementById("totalInterestDisplay").innerText = formatter.format(totalInterest);
document.getElementById("totalCostDisplay").innerText = formatter.format(totalCost);
// 9. Show Results
document.getElementById("results").style.display = "block";
}
Understanding Your Mortgage Calculation
Buying a home is often the largest financial decision a person will make. Using a Mortgage Payment Calculator is an essential step in the home-buying process. It helps you understand not just what you can afford today, but how interest rates and loan terms impact your finances over the next 15 to 30 years.
Pro Tip: Even a small difference in your interest rate can save you tens of thousands of dollars over the life of your loan. Use the calculator above to see how a 0.5% rate reduction changes your "Total Interest Paid."
How the Mortgage Formula Works
Your monthly mortgage payment is calculated using an amortization formula that ensures your loan is paid off completely by the end of the term. The formula used in this calculator considers three main variables:
Principal (P): This is the loan amount, calculated as your Home Price minus your Down Payment.
Interest Rate (r): The annual percentage rate charged by the lender. In the calculation, this is divided by 12 to get the monthly rate.
Number of Payments (n): The total number of months you will be paying the loan (e.g., 30 years × 12 months = 360 payments).
Principal vs. Interest
In the early years of a mortgage, the majority of your monthly payment goes toward paying off interest. As time passes, a larger portion applies to the principal balance. This calculator provides a snapshot of the "Total Interest Paid," which often surprises first-time buyers. For example, on a 30-year loan, it is common for the total interest paid to nearly equal the original loan amount, depending on the rate.
Choosing the Right Loan Term
This calculator allows you to toggle between 15-year and 30-year terms. While a 30-year mortgage offers lower monthly payments, it results in significantly higher total interest costs. A 15-year mortgage increases the monthly payment but allows you to build equity much faster and save money in the long run.
What is Included in This Estimate?
This calculation covers Principal and Interest (P&I). Please note that your actual monthly housing costs will likely be higher. Most lenders require an escrow account for:
Property Taxes: Usually 1-2% of the home's value annually.
Homeowners Insurance: Protects your property against damage.
PMI (Private Mortgage Insurance): Required if your down payment is less than 20%.
When planning your budget, ensure you add these costs to the figure generated above to get a complete picture of your affordability.