# Net Present Value / NPV Formula for JavaScript (ES5/ES6)

I tried to find a working NPV formula for JavaScript that would be easy to implement. I couldn’t find one. So I created one myself – here it is 🙂 Mikael Toivio, September 25, 2018

The NPV formula is one way to calculate the Net Present Value (NPV) of cash flows combined with a specified discount rate. The NPV formula is useful for financial analysis and financial modeling. It determines the value of an investment (a company, a project, a cost-saving initiative, etc.)

If you really have a bigger project where you are working with financial calculations then it might be smart to check out finance.js. The NPV is one of its functions and can be found here.

### The Function

Copy/paste this function into your JavaScript and you should be good to go. Nothing extra is needed.

#### JavaScript ES5 function

```/**
* Calculates the Net Present Value of a given initial investment
* cost and an array of cash flow values with the specified discount rate.
*
* @param {number} rate - The discount rate percentage
* @param {number} initialCost - The initial investment
* @param {array} cashFlows - An array of future payment amounts
* @return {number} The calculated Net Present Value
*/
function getNPV(rate, initialCost, cashFlows) {
var npv = initialCost;

for (var i = 0; i < cashFlows.length; i++) {
npv += cashFlows[i] / Math.pow(rate / 100 + 1, i + 1);
}

return npv;
}
```

#### JavaScript ES6 function

```/**
* Calculates the Net Present Value of a given initial investment
* cost and an array of cash flow values with the specified discount rate.
*
* @param {number} rate - The discount rate percentage
* @param {number} initialCost - The initial investment
* @param {array} cashFlows - An array of future payment amounts
* @return {number} The calculated Net Present Value
*/
function getNPV(rate, initialCost, cashFlows) {
return cashFlows.reduce(
(accumulator, currentValue, index) =>
accumulator + currentValue / Math.pow(rate / 100 + 1, index + 1),
initialCost
);
}
```

### Using the function

```var rate = 10;
var initialCost = -25000;
var cashFlows = [-10000, 0, 10000, 30000, 100000];

console.log(getNPV(rate, initialCost, cashFlows));
// expected output: 56004.77488497429
```

rate: is the discount rate. This is your discount rate as a percentage value (10 is a 10% discount).

initialCost: is the initial payment. It is often a negative number but can also be positive.

cashFlows: is the Future Payment amounts. It is a negative/positive number. There can be as many cash-flow variables within the array passed into the function. Each representing a year.

## Deeper understanding of what is Net Present Value / NPV Formula?

For understanding the actual formula I am not the person to do the explaining. One page that helped me understand the formula and also helped me create this function was this page.

NPV Formula is widely used and can be found implemented in Apple Numbers, Excel Sheets and Google Sheets out of the box.

Hope this helped you forward 🙂