Emmanuel Akinfulubi

JavaScriptBasic

Calculate simple and compound interest in JavaScript

Intermediate · 4 min read

Put money in a savings account and the bank pays you interest: extra money for letting it hold yours. Take a loan and you pay interest instead. Savings apps, loan calculators and investment dashboards all show these figures, and they all use one of two methods: simple interest or compound interest.

In this tutorial you will work out both for a deposit of ₦100,000 at 10% a year for three years, and see exactly where the difference comes from. Amounts are kept in kobo (100 kobo make one naira) as whole numbers. You need to know what a function is, what Math.round does, and that ** means "to the power of" (2 ** 3 is 8).

Simple interest

With simple interest, you earn the same amount every year, always worked out on the money you first put in. That first amount is called the principal.

10% of ₦100,000 is ₦10,000, so each year you earn ₦10,000. Over three years that is ₦30,000. The formula is principal times rate times years.

Compound interest

With compound interest, each year's interest is added to the balance, and next year's interest is worked out on that bigger balance. You earn interest on your interest.

  • Year 1: 10% of ₦100,000 is ₦10,000. Balance ₦110,000.
  • Year 2: 10% of ₦110,000 is ₦11,000. Balance ₦121,000.
  • Year 3: 10% of ₦121,000 is ₦12,100. Balance ₦133,100.

Each year the balance is multiplied by 1.1 (all of it, plus 10%). Three years is 1.1 times 1.1 times 1.1, which you can write as 1.1 ** 3.

Here is the exercise. Try it yourself first if you like, or read on and we will build it together.

66 Simple interest and compound interest

fair

On a deposit of ₦100,000 at 10% for three years, work out the simple interest and the compound interest, and say where the gap comes from.

Stuck? Three hints, smallest first
  1. 1. Simple interest is the same amount every year: principal × rate × years.
  2. 2. Compound interest earns on the interest, so the balance is principal × (1 + rate) ** years.
  3. 3. Compounding more often than yearly means dividing the rate and multiplying the periods.

Step 1: simple interest in code

Write the principal in kobo and the rate as a decimal (10% is 0.1):

Step 1

const principal = 100_000_00;  // 100,000 naira in kobo
const rate = 0.1;
const simpleInterest = (kobo, r, years) => Math.round(kobo * r * years);
console.log(simpleInterest(principal, rate, 3)); // 3000000
console.log(simpleInterest(principal, rate, 6)); // 6000000

The underscores in 100_000_00 only make the number easier to read: naira then kobo. 3,000,000 kobo is ₦30,000. Doubling the years doubles simple interest, because it grows by the same amount every year.

Step 2: compound interest in code

The balance after some years is kobo * (1 + r) ** years. The interest is the balance minus what you put in:

Step 2

const principal = 100_000_00;
const rate = 0.1;
const compoundInterest = (kobo, r, years) => {
  const balance = kobo * (1 + r) ** years;
  return Math.round(balance) - kobo;
};
console.log(compoundInterest(principal, rate, 3)); // 3310000
console.log(compoundInterest(principal, rate, 1)); // 1000000

₦33,100 of interest, against ₦30,000 for simple interest. The ₦3,100 gap is the interest earned on interest: ₦1,000 in year 2 and ₦2,100 in year 3. Over one year the two methods give the same answer, because there is no earlier interest to earn on yet.

Step 3: round once, at the end

The balance is worked out in full and rounded only once. Look at the raw number:

JavaScript

console.log(100_000_00 * 1.1 ** 3); // 13310000.000000004

The tiny tail comes from decimals stored in binary. Rounding at the very end removes it. If you rounded the balance every year instead, small errors could build up, and your figure might not match the bank's statement.

Step 4: compounding more often

Many accounts add interest monthly. Split the yearly rate into 12 parts and use 12 periods a year:

Step 4

const principal = 100_000_00;
const rate = 0.1;
const compoundInterest = (kobo, r, years, perYear = 1) => {
  const periods = years * perYear;
  const balance = kobo * (1 + r / perYear) ** periods;
  return Math.round(balance) - kobo;
};
console.log(compoundInterest(principal, rate, 3));     // 3310000
console.log(compoundInterest(principal, rate, 3, 12)); // 3481818
console.log(compoundInterest(principal, rate, 0));     // 0

perYear = 1 is a default parameter: if you leave it out, interest is added once a year, the same as Step 2. Monthly compounding earns ₦34,818.18, more again, because interest starts earning interest sooner. This is why a loan advert can say "10% a year" while the real yearly cost is higher: compounded monthly, 10% works out to about 10.47% a year.

The full solution

The answer panel has both functions, prints simple, yearly compound and monthly compound interest over three years, and the one-year case where they match. Its checks also cover zero years and what happens when you double the years. Press Run to see the output.

The answer

const principal = 100_000_00;          // 100,000 naira in kobo
const rate = 0.1;                      // 10% a year

const simpleInterest = (kobo, r, years) => Math.round(kobo * r * years);

const compoundInterest = (kobo, r, years, perYear = 1) => {
  const periods = years * perYear;
  const balance = kobo * (1 + r / perYear) ** periods;
  return Math.round(balance) - kobo;
};

console.log('simple, 3 years   ->', simpleInterest(principal, rate, 3), 'kobo');
console.log('compound, 3 years ->', compoundInterest(principal, rate, 3), 'kobo');
console.log('compound monthly  ->', compoundInterest(principal, rate, 3, 12), 'kobo');
console.log('one year, both    ->', simpleInterest(principal, rate, 1), compoundInterest(principal, rate, 1));

Output


      
8 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 8 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

8 checks

test('simple interest is 30,000 naira', simpleInterest(principal, rate, 3), 3000000);
test('compound interest is more', compoundInterest(principal, rate, 3), 3310000);
test('the gap is interest earned on interest', compoundInterest(principal, rate, 3) - simpleInterest(principal, rate, 3), 310000);
test('over one year they are identical', simpleInterest(principal, rate, 1) === compoundInterest(principal, rate, 1), true);
test('compounding monthly earns more again', compoundInterest(principal, rate, 3, 12), 3481818);
test('zero years earns nothing either way', [simpleInterest(principal, rate, 0), compoundInterest(principal, rate, 0)], [0, 0]);
test('doubling the years doubles simple interest', simpleInterest(principal, rate, 6), 2 * simpleInterest(principal, rate, 3));
test('but more than doubles compound interest', compoundInterest(principal, rate, 6) > 2 * compoundInterest(principal, rate, 3), true);

The two are the same over one period and diverge after that, because compounding pays interest on interest. Over three years at 10% the difference is ₦3,100 on ₦100,000; over thirty years it is the entire point of a savings account.

Note where the rounding sits. The balance is computed in full precision and rounded once, at the end, rather than rounded each year — rounding per period would drift, and on a real ledger it would also disagree with the bank's own statement. Compounding monthly rather than annually turns a nominal 10% into an effective 10.47%, which is why loan adverts quote the nominal rate and the small print quotes the effective one.

Common mistakes

  • Writing the rate as 10 instead of 0.1. (1 + 10) ** 3 is 1,331 times your money. Percentages become decimals first.
  • Rounding the balance every year. Work in full precision and round once at the end.
  • Forgetting to subtract the principal. kobo * (1 + r) ** years is the final balance, not the interest.

Try it yourself

  1. Work out the compound interest on ₦100,000 at 10% for 30 years, and compare it with simple interest.
  2. Write balanceAfter(kobo, r, years) that returns the balance instead of the interest.
  3. Use a loop to find how many years it takes ₦100,000 at 10% compound to at least double.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.