Please fill in all fields with valid numeric values.
Estimated Monthly Payment (P&I)
$0.00
Loan Amount
$0.00
Total Interest Paid
$0.00
Total Cost of Loan
$0.00
function calculateMortgage() {
// 1. Get inputs by exact IDs
var homePriceInput = document.getElementById('home-price').value;
var downPaymentInput = document.getElementById('down-payment').value;
var loanTermInput = document.getElementById('loan-term').value;
var interestRateInput = document.getElementById('interest-rate').value;
var errorMsg = document.getElementById('calc-error-msg');
var resultsContainer = document.getElementById('calc-results');
// 2. Validate inputs
if (homePriceInput === "" || downPaymentInput === "" || loanTermInput === "" || interestRateInput === "") {
errorMsg.style.display = "block";
resultsContainer.style.display = "none";
return;
}
// Parse values to floats to ensure math works correctly
var homePrice = parseFloat(homePriceInput);
var downPayment = parseFloat(downPaymentInput);
var loanTermYears = parseFloat(loanTermInput);
var annualRatePercentage = parseFloat(interestRateInput);
// Additional validation for negative numbers or nonsense inputs
if (isNaN(homePrice) || isNaN(downPayment) || isNaN(loanTermYears) || isNaN(annualRatePercentage) || homePrice < 0 || downPayment < 0 || loanTermYears <= 0 || annualRatePercentage < 0) {
errorMsg.textContent = "Please enter positive, valid numbers.";
errorMsg.style.display = "block";
resultsContainer.style.display = "none";
return;
}
// Hide error message if validation passes
errorMsg.style.display = "none";
resultsContainer.style.display = "block";
// 3. Perform Calculation Logic
// Principal Loan Amount = Home Price – Down Payment
var principal = homePrice – downPayment;
if (principal <= 0) {
errorMsg.textContent = "Down payment cannot equal or exceed the home price.";
errorMsg.style.display = "block";
resultsContainer.style.display = "none";
return;
}
// Monthly Interest Rate = (Annual Rate / 100) / 12
var monthlyRate = (annualRatePercentage / 100) / 12;
// Total Number of Payments = Loan Term Years * 12
var numberOfPayments = loanTermYears * 12;
var monthlyPayment = 0;
// Handle edge case where interest rate is 0
if (monthlyRate === 0) {
monthlyPayment = principal / numberOfPayments;
} else {
// Standard Mortgage Formula: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1 ]
// Math.pow(base, exponent) is used for (1 + i)^n
var combinedRateStr = Math.pow((1 + monthlyRate), numberOfPayments);
monthlyPayment = principal * ((monthlyRate * combinedRateStr) / (combinedRateStr – 1));
}
// Calculate total cost and total interest
var totalCost = monthlyPayment * numberOfPayments;
var totalInterest = totalCost – principal;
// 4. Output results with formatting
// Use helper function to format currency
function formatCurrency(num) {
return "$" + num.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
}
document.getElementById('monthly-payment-result').textContent = formatCurrency(monthlyPayment);
document.getElementById('loan-amount-result').textContent = formatCurrency(principal);
document.getElementById('total-interest-result').textContent = formatCurrency(totalInterest);
document.getElementById('total-cost-result').textContent = formatCurrency(totalCost);
}
Understanding Your Mortgage Payment
Buying a home is likely the largest financial transaction of your life. Understanding how your monthly mortgage payment is calculated is crucial for budgeting and determining affordability. This tool helps you estimate your monthly Principal and Interest (P&I) payments based on standard loan parameters.
How Mortgage Amortization Works
A typical fixed-rate mortgage is "amortized." This means your monthly payment remains the same total amount over the life of the loan (e.g., 30 years), but the portion that goes toward interest versus principal changes over time.
Early Years: In the beginning of your loan term, the vast majority of your payment goes toward paying off interest, with only a small fraction reducing your principal balance.
Later Years: As the principal balance decreases, less interest accrues. Eventually, the majority of your payment goes toward the principal, accelerating equity build-up.
Note: The calculator above calculates P&I only. Your actual monthly payment to your lender may also include escrow costs for property taxes, homeowners insurance, and potentially private mortgage insurance (PMI).
Key Factors Affecting Your Payment
Four main components determine the size of your monthly mortgage bill:
Home Price & Down Payment: The difference between the home's price and your cash down payment determines your "Principal" loan amount. A larger down payment means a smaller loan and lower monthly payments.
Interest Rate: This is the cost of borrowing money, expressed as an annual percentage. Even a small difference in percentage points (e.g., 4.5% vs 5.0%) can significantly impact your monthly payment and the total interest paid over the life of the loan.
Loan Term: The length of time you have to repay the loan. A 30-year term is standard, offering lower monthly payments but higher total interest costs. A 15-year term usually has lower interest rates and vastly reduces total interest paid, but requires much higher monthly payments.
Realistic Mortgage Calculation Example
Let's look at a realistic scenario for a first-time homebuyer using the inputs from the calculator above:
Home Price: $350,000
Down Payment: $70,000 (This is a 20% down payment, often ideal for avoiding PMI)
Loan Amount (Principal): $280,000
Loan Term: 30 Years
Interest Rate: 5.25%
Using the standard mortgage formula, the estimated monthly Principal & Interest payment would be approximately $1,546.16. Over the full 30 years, you would pay a total of $276,617.60 in interest alone, making the total cost of the $280,000 loan over half a million dollars ($556,617.60).
Use the calculator at the top of this page to adjust these figures based on current market rates and your personal budget.