1 Stop Mortgage Calculator - Calculate your Mortgage payments in Seconds!
JavaScript mortgage calculator formula and code

# 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:

## How do we 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 mortgage calculator formula means that the regular 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) (ouch!)

### 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, the Javascript mortgage calculator uses the Math.pow(x, y) Javascript 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)

## Simple javascript mortgage calculator loan payment javascript function example:

Just copy and paste the following JavaScript code (in blue) in a html page, after the <body> tag, but before the ending </body> tag.

<script language="JavaScript" type="text/javascript">
<!-- hide the mortgage calculator formula from non JavaScript browsers

function find_payment(PR, IN, PE) {
var PAY = (PR * IN) / (1 - Math.pow(1 + IN, -PE))
return PAY
}

var principal = 200000
var interest = 0.09
var term = 30
var monthly_payment = find_payment(principal, interest / 12, term * 12)

alert("Amount of the loan:\t\$" + principal + "\n" +
"Annual interest rate:\t" + interest * 100 + "%\n" +
"Term of the mortgage loan:\t" + term + " years\n\n" +
"Monthly payment:\t\$" + monthly_payment)

//--> End of hidden JavaScript for Browsers not supporting it
</script>

The Javascript function find_payment() does the heavy javascript mortgage calculator calculations here. It takes as arguments the three terms required by the expression: PR, IN, and PE (mortgage principal amount, mortgage interest rate and mortgage periods in years).

The result of the find_payment function code is stored in the Javascript PAY variable, which is then transferred to the monthly_payment variable.

Finally, the result is shown in an alert box.

This Javascript mortgage calculator script example uses fixed values: principal, interest and term (hand coded in the above example). Those fixed values are passed to the find_payment function.

Before the javascript mortgage calculator find_payment function does it’s calculations, the interest rate is divided by 12 and the term is multiplied by 12. This is necessary because those values were annual values, not monthly.

 House for sale secrets revealed! Attention Homesellers! Find Out How To Sell houses faster

The Canadians do not calculate their mortgage payments the same way as the rest of the world. The US and, in fact, nearly every other country of the world compound the rates by payment periods. Not so in Canada. Over there, the rates are compounded semi-annually.

## How accurate can a javascript mortgage calculator be?

A javascript mortgage calculator is simply the combination of an html form (to enter the user's data) and some javascript script code that works on the form's data. There are many ways to do the javascript mortgage calculations, but you must remember that when javascript is involved, it's the user's computer that does the work, not the web server.

You must also understand that the results given by an online JavaScript mortgage calculator lack real-world reliability and can vary from one javascript mortgage calculator to the next. This site included. Why? First because some javascript rouding routines are involved and, most importantly, because not one loan is identical. For example, some lending banks and lending institutions add mortgage insurance (at different rates) while others don't.

However, the basic mortgage financial formula is always the same (see "how to code a mortgage calculator"). There are some slight variations. For example, while the rates of a US mortgage are compounded monthly, the Canadian mortgage rates are compounded semi-annually. The javascript formula (and code) is therefore different if you program a Canadian mortgage calculator. The example, below, is for US mortgage calculations, i.e., where the rates of the mortgage loan are compounded monthly.

## To what extent can we trust a Javascript mortgage calculator?

Many years ago, people knew nothing about mortgage calculators, and even less about javascript mortgage calculators. In those ancient times their personal mortgage calculator was a pen and a piece of paper. The pocket financial calculators changed all this. And these days, those same hand-held financial calculators are fading away, they are replaced by the online mortgage calculator. In fact, all sorts of javascript calculators can be found on the web.

The online Javascript mortgage calculator can now be found on nearly all the real estate brokers and real estate agents web sites. The banks have them and, of course, so do the mortgage lenders.

A mortgage calculator poses intriguing questions: How much can you afford to borrow to buy a house? How much will your monthly mortgage payments be? How much will your bi-weekly mortgage payments be? Should you refinance your mortgage loan? And so on.

How accurate can a javascript mortgage calculator be? In fact, are they accurate? Yes and no. A mortgage calculator puts user-entered data into complex financial equations that would be intimidating for the average not-mathematically-inclined person to solve by hand.

However, the results given by an online Javascript mortgage calculator lack real-world accuracy and can vary from one mortgage calculator to the next. This site's included.