- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- 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 Articles
- Java Program to calculate Simple Interest and Compound Interest
- Swift Program to Calculate simple interest and compound interest
- Haskell Program to Calculate simple interest and compound interest
- Kotlin Program to calculate Simple Interest and Compound Interest
- C++ Program to Calculate simple interest and compound interest
- Java Program to calculate Compound Interest
- Swift Program to Calculate Compound Interest
- Kotlin Program to Calculate Compound Interest
- Java Program to Calculate the Compound Interest
- How to Calculate Compound Interest using Golang code?
- Compare simple interest and compound interest.
- What is compound interest?
- Python Program for compound interest
- C Program for compound interest?
- Program to find compound interest in C++

Advertisements