function calculateMortgage() {
// Get inputs
var homePrice = parseFloat(document.getElementById('homePrice').value);
var downPayment = parseFloat(document.getElementById('downPayment').value);
var interestRate = parseFloat(document.getElementById('interestRate').value);
var loanTermYears = parseInt(document.getElementById('loanTerm').value);
var annualTax = parseFloat(document.getElementById('propertyTax').value);
var annualInsurance = parseFloat(document.getElementById('homeInsurance').value);
// Basic Validation
if (isNaN(homePrice) || isNaN(downPayment) || isNaN(interestRate) || isNaN(loanTermYears)) {
alert("Please enter valid numbers for Home Price, Down Payment, and Interest Rate.");
return;
}
// Defaults for optional fields if empty/NaN
if (isNaN(annualTax)) annualTax = 0;
if (isNaN(annualInsurance)) annualInsurance = 0;
// Calculations
var principal = homePrice – downPayment;
if (principal <= 0) {
alert("Down payment cannot be greater than or equal to the home price.");
return;
}
var monthlyInterestRate = (interestRate / 100) / 12;
var numberOfPayments = loanTermYears * 12;
// Monthly Principal & Interest (M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1 ])
var monthlyPI = 0;
if (interestRate === 0) {
monthlyPI = principal / numberOfPayments;
} else {
monthlyPI = principal * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments)) / (Math.pow(1 + monthlyInterestRate, numberOfPayments) – 1);
}
var monthlyTax = annualTax / 12;
var monthlyInsurance = annualInsurance / 12;
var totalMonthlyPayment = monthlyPI + monthlyTax + monthlyInsurance;
var totalInterest = (monthlyPI * numberOfPayments) – principal;
// Update DOM
document.getElementById('resPI').innerText = "$" + monthlyPI.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('resTax').innerText = "$" + monthlyTax.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('resIns').innerText = "$" + monthlyInsurance.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('resTotal').innerText = "$" + totalMonthlyPayment.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('resTotalInterest').innerText = "$" + totalInterest.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
// Show results container
document.getElementById('resultsArea').style.display = 'block';
}
Understanding Your Mortgage Calculation
Purchasing a home is likely the largest financial commitment you will make in your lifetime. Understanding how your monthly mortgage payment is calculated is essential for budgeting and long-term financial planning. This calculator breaks down the "PITI" components: Principal, Interest, Taxes, and Insurance.
The Components of Your Monthly Payment
Principal: The portion of your payment that goes toward paying down the money you borrowed. In the early years of a mortgage, this amount is small, but it grows over time.
Interest: The fee you pay to the lender for borrowing the money. Higher interest rates significantly increase your monthly payment and the total cost of the loan.
Property Taxes: Assessed by your local government to fund public services. This calculator estimates the monthly set-aside required for these annual bills.
Homeowners Insurance: Protects your property against damage. Lenders typically require this to be paid monthly via an escrow account.
How Interest Rates Impact Affordability
Even a small difference in interest rates can have a massive impact on your purchasing power. For example, on a $350,000 loan, the difference between a 6% and a 7% interest rate can change your monthly payment by over $200 and cost you tens of thousands of dollars in additional interest over the life of a 30-year loan.
Why the Down Payment Matters
Your down payment reduces the principal loan amount immediately. A larger down payment (typically 20% or more) not only lowers your monthly Principal & Interest payment but may also help you avoid Private Mortgage Insurance (PMI), a fee lenders charge borrowers with less equity.
Use the tool above to experiment with different scenarios. Try adjusting the Loan Term to see how a 15-year mortgage increases monthly payments but drastically reduces the total interest paid.