# Javascript mortgage calculator formula and code example

//Javascript mortgage calculator formula and code example

## Javascript mortgage calculator formula and code example

### What is a Javascript mortgage calculator?

A Javascript mortgage calculator is a javascript program that calculates online the monthly mortgage payments (or bi-weekly mortgage payments) one would pay if one would contract a loan (the principal) at a fixed interest rate and at a fixed loan term (10, 15, 20 or 30 years).

These four loan calculators will automatically calculate everything for you:
Us Mortgage Calculator With Amortization
Accelerated Bi-weekly Mortgage vs Monthly Mortgage Calculator
Small US Mortgage Calculator
How Much Can I Borrow – Mortgage Calculator

### How to code a Javascript mortgage calculator?

In order to do some javascript calculations, the javascript mortgage calculator will need some variables:

FV (future value of the mortgage loan)
In order to figure out the payments, the script will have to calculate the future value of the mortgage loan (by doing calculations on other javascript variables).

IN (interest rate of the mortgage loan)
The interest rate is a needed variable. It is usually known, however, when we have all the other variables, it’s easy to calculate. See this mortgage rate calculator for an interest calculator example.

PE (number of periods of the mortgage loan)
The number of periods of the mortgage loan (either in years, in months or weeks) is how many payments will be made to repay the loan.

PAY (regular payment to apply to the mortgage loan)
This is what people is really looking for: the regular mortgage payment to apply to the mortgage loan.

PR(present value of the mortgage loan)
The present value of the mortgage loan… or mortgage principal, or loan principal) (how much ia borrowed).

### How to code a mortgage calculator:

General formula used by the javascript mortgage calculator to calculate the specifics of the mortgage loan:
PAY = PR x IN / (1 – (1 + IN)–PE)
This javascript formula means that the regular mortgage monthly payment (PAY) is equal to the present value of the mortgage loan (PR) multiplied by the mortgage interest rate (IN) divided by 1 minus 1 plus the interest rate raised at the power of the number of mortgage periods (PE) (try to say that when you just woke up in the morning!)

Here’s how this javascript mortgage calculator formula looks as a javascript expression:
PAY = (PR * IN) / (1 – Math.pow(1 + IN, -PE))

As you can see in the above source code extracted from one of my mortgage calculators, this Javascript calculator uses the Math.pow(x, y) function.

In Math.pow (x, y) x is the base number and y is the exponent (the power to which the base number is raised). For example, Math.pow(3, 2) raises 3 to the power of 2, which gives 9.

Our javascript mortgage calculator formula is fine, except for one small detail, we need to find out the monthly mortgage payments, not the annual mortgage payments (bi-weekly mortgage payments calculations are also possible).

This means that the javascript mortgage calculator will have to divide the interest rate (IN) by 12 (converting the annual mortgage interest rate into a monthly mortgage interest rate)

The mortgage calculator will also have to convert the number of periods (PE) into monthly mortgage loan payment periods by multiplying it by 12.

Finaly, in order to do it’s calculations, the javascript mortgage calculator will need the following input values:

PR (present value of the mortgage loan or principal amount)
IN (annual interest rate of the mortgage loan)
PE (number of periods of the mortgage loan in years, or loan term in years)