- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Calculate compound interest in JavaScript

## Compound Interest Formula

Compound interest is calculated using the following formula −

CI = P*(1 + R/n) (nt) – P

Here,

- P is the principal amount.
- R is the annual interest rate.
- t is the time the money is invested or borrowed for.
- n is the number of times that interest is compounded per unit t, for example if interest is compounded monthly and t is in years then the value of n would be 12. If interest is compounded quarterly and t is in years then the value of n would be 4.

We are required to write a JavaScript function that takes in principal, rate, time, and the number n and calculates the compound interest.

## Example

Let’s write the code for this function −

const principal = 2000; const time = 5; const rate = .08; const n = 12; const compoundInterest = (p, t, r, n) => { const amount = p * (Math.pow((1 + (r / n)), (n * t))); const interest = amount - p; return interest; }; console.log(compoundInterest(principal, time, rate, n));

## Output

The output in the console: −

979.6914166032097

- Related Questions & Answers
- Java Program to calculate Compound Interest
- Java Program to calculate Simple Interest and Compound Interest
- Java Program to Calculate the Compound Interest
- Compare simple interest and compound interest.
- C Program for compound interest?
- Python Program for compound interest
- C Program for the compound interest?
- Program to find compound interest in C++
- Java Program to Calculate Simple Interest
- Calculate interest amount by using formula in C language
- C++ program to find the rate percentage from compound interest of consecutive years
- Compound Literals in C
- Compound operators in Arduino
- Compare fixed interest rate and floating interest rate
- Differentiate between fixed interest rates and floating interest rates.

Advertisements