A mortgage is a long-term loan used to purchase real estate, typically a home. The monthly payment you make on a mortgage is calculated using a specific formula that considers the principal loan amount, the annual interest rate, and the loan term (the duration of the loan).
Key Components:
Loan Amount: This is the total amount of money you borrow from the lender to buy your property. It's the principal of your loan.
Annual Interest Rate: This is the percentage charged by the lender for borrowing the money. It's usually expressed as a yearly rate.
Loan Term: This is the total number of years you have to repay the loan. Common terms are 15, 20, or 30 years.
The Calculation:
The standard formula for calculating the monthly mortgage payment (M) is:
M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
Where:
P = Principal loan amount
i = Monthly interest rate (Annual interest rate / 12)
n = Total number of payments (Loan term in years * 12)
This calculator helps you estimate this monthly payment and also shows the total amount of principal and interest you'll pay over the life of the loan.
function calculateMortgage() {
var principal = parseFloat(document.getElementById("loanAmount").value);
var annualRate = parseFloat(document.getElementById("interestRate").value);
var years = parseFloat(document.getElementById("loanTerm").value);
var monthlyPaymentElement = document.getElementById("monthlyPayment");
var totalPrincipalElement = document.getElementById("totalPrincipal");
var totalInterestElement = document.getElementById("totalInterest");
var totalAmountPaidElement = document.getElementById("totalAmountPaid");
// Clear previous results
monthlyPaymentElement.textContent = "$0.00";
totalPrincipalElement.textContent = "$0.00";
totalInterestElement.textContent = "$0.00";
totalAmountPaidElement.textContent = "$0.00";
// Input validation
if (isNaN(principal) || principal <= 0 ||
isNaN(annualRate) || annualRate < 0 ||
isNaN(years) || years <= 0) {
alert("Please enter valid positive numbers for all fields.");
return;
}
var monthlyRate = annualRate / 100 / 12;
var numberOfPayments = years * 12;
var monthlyPayment = 0;
var totalAmountPaid = 0;
var totalInterest = 0;
var totalPrincipal = principal; // Principal remains the same
if (monthlyRate === 0) { // Handle 0% interest rate case
monthlyPayment = principal / numberOfPayments;
} else {
monthlyPayment = principal * (monthlyRate * Math.pow(1 + monthlyRate, numberOfPayments)) / (Math.pow(1 + monthlyRate, numberOfPayments) – 1);
}
totalAmountPaid = monthlyPayment * numberOfPayments;
totalInterest = totalAmountPaid – principal;
// Format currency
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
monthlyPaymentElement.textContent = formatter.format(monthlyPayment);
totalPrincipalElement.textContent = formatter.format(totalPrincipal);
totalInterestElement.textContent = formatter.format(totalInterest);
totalAmountPaidElement.textContent = formatter.format(totalAmountPaid);
}