Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Sat Apr 08 2023

How to calculate change in percentage using JavaScript

Getting the change in percentage can be useful in many cases. For example, you want to see how many percentage a stock has increased or decreased the last 5 years, or maybe you want to see how much your cool app has grown compared to last month. Regardless of area, we almost always come across the scenario where we want to get the percentage diff. In todays guide, we will implement this formula in JavaScript.

Formula

The formula for calculating the change is as follows:

(finalValue - originalValue) / originalValue × 100

So no rocket science here. But still so useful!

Implement in JavaScript

const getPercentageChange = (originalValue, finalValue) => { return (((finalValue - originalValue) / originalValue) * 100).toFixed(2); };

Let's test it

console.log(getPercentageChange(10, 20)); // 100 console.log(getPercentageChange(20, 10)); // -50 console.log(getPercentageChange(130, 520)); // 300 console.log(getPercentageChange(85, 10)); // -88.24

Outro

In todays tutorial, we shared how we can implement the percentage change formula in JavaScript. Note that this should not be confused with percentage difference formula.

Hope you enjoyed this short guide, see you in the next one!

signatureSat Apr 08 2023
See all our articles