Home > Articles > Web Design & Development > Javascript

Performing Financial Calculations

  • Print
  • + Share This
Paul McFedries teaches you how to use JavaScript to perform a number of basic financial calculations, including loan or mortgage payments, the future value of an investment, and inflation.
This article is excerpted from Special Edition Using JavaScript.
From the author of

JavaScript doesn't have any built-in financial functions for calculating things like loan payments and future values. However, the formulas for many financial calculations combine the four basic arithmetic operations—addition, subtraction, multiplication, and division—with exponentiation, raising one number to the power of another. The latter is handled by the Math.pow() method:

Math.pow(Number1, Number2)


The base number


The exponent (that is, the power to which Number1 is raised)

For example, Math.pow(4, 2) raises 4 to the power of 2, which gives 16.

In the sections that follow, I'll show you how to use JavaScript to perform a number of basic financial calculations. Along the way, I'll provide the general formulas for each calculation, and these formulas will use the terms in Table 1.

Table 1 Terms Used in the Financial Formulas


What It Means


The future value of an investment


The interest rate for an investment or loan


The number of periods in an investment or loan


The regular payment into an investment or loan


The present value of an investment or loan

Calculating Loan or Mortgage Payments

Loans and mortgages are a fact of financial life, and it's a rare household that doesn't have or need a loan of some kind. An easy way to add some value to your site is to set up a kind of loan or mortgage calculator where users can enter the specifics of the loan and your script returns the monthly payment.

Here's the general formula for this kind of calculation:

PMT = PV x IR / (1 – (1 + IR)NP)

Here's how this looks as a JavaScript expression:

PMT = (PV * IR) / (1 - Math.pow(1 + IR, -NP))

Listing 1 puts this expression through its paces.

Listing 1: Calculating a Loan Payment

<script language="JavaScript" type="text/javascript">

function calculate_payment(PV, IR, NP) {
  var PMT = (PV * IR) / (1 - Math.pow(1 + IR, -NP))
  return round_decimals(PMT, 2)

function round_decimals(original_number, decimals) {
  var result1 = original_number * Math.pow(10, decimals)
  var result2 = Math.round(result1)
  var result3 = result2 / Math.pow(10, decimals)
  return (result3)

var present_value = 200000
var interest_rate = 0.09
var loan_term = 30
var monthly_payment = calculate_payment(present_value, 
[ic:ccc]interest_rate / 12, loan_term * 12)

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


The function calculate_payment() does the heavy lifting here. Notice how it takes as arguments the three terms required by the expression: PV, IR, and NP. The loan payment result is stored in the PMT variable, which is then sent to the round decimals() function to round it to two decimal places.

In the setup for the function, the variables present_value, interest_rate, and loan_term are initialized and then passed to the calculate_payment() function.


When the values are passed to the calculate_payment() function, notice that the interest_rate is divided by 12, and loan_term is multiplied by 12. You need to do this because the original values supplied to these variables are annual values, so they must be converted into monthly numbers before getting to the function.

  • + Share This
  • 🔖 Save To Your Account