When we review feasibility studies, the payback period is often the number investors read first. It is also one of the easiest to get subtly wrong. Here is a small, dependency-free JavaScript snippet that computes both the simple and the discounted payback period, including the fractional year, from a list of annual cash flows.

The idea

  • Simple payback: how many years until the cumulative cash flow turns positive.
  • Discounted payback: the same, but each year's cash flow is first discounted at a chosen rate, so money received later counts for less.

The discounted version is always equal to or longer than the simple one. If the gap between them is large, the project relies heavily on cash that arrives late.

The code

// cashFlows[0] is the initial investment (negative), then one value per year. function paybackPeriod(cashFlows, rate = 0) { let cumulative = 0; for (let t = 0; t < cashFlows.length; t++) { const cf = cashFlows[t] / Math.pow(1 + rate, t); const before = cumulative; cumulative += cf; if (t > 0 && before < 0 && cumulative >= 0) { // fraction of year t needed to recover the remaining amount return (t - 1) + (-before / cf); } } return null; // not recovered within the horizon } const flows = [-1000000, 250000, 300000, 350000, 350000, 350000]; console.log('Simple payback (years):', paybackPeriod(flows).toFixed(2)); console.log('Discounted payback @10% (years):', paybackPeriod(flows, 0.10).toFixed(2)); 

Running it prints:

Simple payback (years): 3.29 Discounted payback @10% (years): 4.10 

The cash flows above are an illustrative example, not data from a real project.

Things to watch

  1. Year-zero convention. The function assumes the investment happens at t = 0 and the yearly flows arrive at the end of each year. If your model uses mid-year flows, adjust the discount exponent.
  2. Returning null. If the project never recovers its cost within the horizon, say so explicitly in the report instead of extrapolating.
  3. Do not read payback alone. It ignores everything after the recovery point. Read it together with NPV, IRR and a sensitivity test on sales.

We use checks like this when reviewing the financial section of feasibility studies. More on the financial indicators we look at: googanconsult.com.