function calculateMortgage() {
// Get inputs
var homePrice = parseFloat(document.getElementById('mpcHomePrice').value);
var downPayment = parseFloat(document.getElementById('mpcDownPayment').value);
var interestRate = parseFloat(document.getElementById('mpcInterestRate').value);
var loanTermYears = parseInt(document.getElementById('mpcLoanTerm').value);
var annualTax = parseFloat(document.getElementById('mpcPropertyTax').value);
var annualInsurance = parseFloat(document.getElementById('mpcHomeInsurance').value);
var monthlyHOA = parseFloat(document.getElementById('mpcHOA').value);
var resultsDiv = document.getElementById('mpcResults');
var errorDiv = document.getElementById('mpcError');
// Validation
if (isNaN(homePrice) || isNaN(downPayment) || isNaN(interestRate) ||
isNaN(annualTax) || isNaN(annualInsurance) || isNaN(monthlyHOA)) {
errorDiv.style.display = 'block';
resultsDiv.style.display = 'none';
return;
}
if (homePrice <= 0 || downPayment < 0 || interestRate < 0) {
errorDiv.innerText = "Values must be positive.";
errorDiv.style.display = 'block';
resultsDiv.style.display = 'none';
return;
}
// Hide error
errorDiv.style.display = 'none';
// Calculations
var principal = homePrice – downPayment;
// If downpayment is greater than home price
if (principal < 0) {
principal = 0;
}
var monthlyRate = (interestRate / 100) / 12;
var totalPayments = loanTermYears * 12;
var monthlyPrincipalInterest = 0;
// Handle zero interest rate case
if (interestRate === 0) {
monthlyPrincipalInterest = principal / totalPayments;
} else {
// M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1 ]
var x = Math.pow(1 + monthlyRate, totalPayments);
monthlyPrincipalInterest = principal * ((monthlyRate * x) / (x – 1));
}
var monthlyTax = annualTax / 12;
var monthlyInsurance = annualInsurance / 12;
var totalMonthlyPayment = monthlyPrincipalInterest + monthlyTax + monthlyInsurance + monthlyHOA;
var totalAmountPaid = (monthlyPrincipalInterest * totalPayments);
var totalInterest = totalAmountPaid – principal;
// Update DOM
document.getElementById('mpcTotalMonthly').innerText = formatCurrency(totalMonthlyPayment);
document.getElementById('mpcPrincipalInterest').innerText = formatCurrency(monthlyPrincipalInterest);
document.getElementById('mpcMonthlyTax').innerText = formatCurrency(monthlyTax);
document.getElementById('mpcMonthlyInsurance').innerText = formatCurrency(monthlyInsurance);
document.getElementById('mpcMonthlyHOA').innerText = formatCurrency(monthlyHOA);
document.getElementById('mpcLoanAmount').innerText = formatCurrency(principal);
document.getElementById('mpcTotalInterest').innerText = formatCurrency(totalInterest);
// Show results
resultsDiv.style.display = 'block';
}
function formatCurrency(num) {
return '$' + num.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
}
Understanding Your Mortgage Payment
Calculating your monthly mortgage payment is the first critical step in the home-buying process. While the sticker price of a home gives you a general idea of cost, the actual monthly cash flow required involves several layers of expenses. This Mortgage Payment Calculator breaks down not just the loan repayment, but the "PITI" (Principal, Interest, Taxes, and Insurance) that typically makes up a monthly housing bill.
The 4 Key Components of a Mortgage Payment
When you write a check to your mortgage servicer, it covers more than just paying back the money you borrowed. Here is how the costs break down:
- Principal: The portion of your payment that reduces the loan balance. In the early years of a 30-year mortgage, this amount is small compared to interest.
- Interest: The cost of borrowing money. With a 6.5% interest rate on a $350,000 loan, your initial payments will be primarily interest.
- Taxes: Property taxes charged by your local municipality. These are usually held in an escrow account by your lender and paid annually or semi-annually on your behalf.
- Insurance: Homeowners insurance protects the property against damage. Like taxes, this is often bundled into your monthly payment via escrow.
How Interest Rates Impact Affordability
Even a small fluctuation in interest rates can drastically change your purchasing power. For example, on a $300,000 loan, the difference between a 6.0% and a 7.0% interest rate is roughly $200 per month. Over the life of a 30-year loan, that single percentage point can cost you over $70,000 in additional interest payments.
Strategies to Lower Your Monthly Payment
If the estimated payment generated by the calculator is higher than your budget allows, consider these strategies:
- Increase your Down Payment: Putting 20% down avoids Private Mortgage Insurance (PMI) and lowers the principal loan amount.
- Extend the Loan Term: Switching from a 15-year to a 30-year term reduces monthly payments, though you will pay more total interest over time.
- Shop for Lower Insurance: Homeowners insurance rates vary by provider. Shopping around can sometimes save $20-$50 per month.
Using This Calculator for Refinancing
You can also use this tool to determine if refinancing makes sense. Enter your remaining loan balance as the "Home Price" (with $0 down payment) and input current market interest rates. If the resulting "Principal & Interest" payment is significantly lower than what you pay now, refinancing might save you money.