How to Calculate Percentage between Two Numbers in JavaScript

Calculate Percentage between Two Numbers

To calculate the percentage between two numbers, divide one number by the other and multiply the result by 100. This will show what percent the first number is of the second. For example, if you want to know what percent 30 is of 75, you would divide 30 by 75 and then multiply that result by 100. This would give you the answer that 30 is 40% of 75.

Formula to calculate percentage:
(numberOne / numberTwo) * 100

Let’s implement this formula in JavaScript.

function isWhatPercentOf(numberOne, numberTwo) {
  return (numberOne / numberTwo) * 100;
}

console.log(isWhatPercentOf(30, 75)); 

console.log(isWhatPercentOf(20, 75)); 

console.log(isWhatPercentOf(20, 75).toFixed(2));
Implementation of percentage calculation formula in JavaScript.
Implementation of percentage calculation formula in JavaScript.

This code defines a function, isWhatPercentOf(), that takes two numbers as input and returns the first number as a percentage of the second number.

For example, if you divide 25 by 50 and then multiply that answer by 100, you would see that 25 is 50% of 50.

console.log((25 / 50) * 100);
One-liner example of percentage calculation.
One-liner example of percentage calculation.

ℹ️ Note – When calculating percentages, you might need to round to a specific number of digits after the decimal.

The Number.toFixed() method can be used to format a number with a certain number of decimal places.
Example –

const percentage = (20 / 75) * 100;
console.log(percentage); 

const fixed = percentage.toFixed(2);
console.log(fixed);
The Number.toFixed method to format a number with a certain number of decimal places.
The Number.toFixed method to format a number with a certain number of decimal places.


The toFixed() method formats a number using fixed-point notation.

The toFixed() method returns a string representation of a number, with a specified number of decimal places. If the number doesn’t have any decimal places, it will be padded with zeros.

const percentage = (50 / 100) * 100;
console.log(percentage); 

const fixed = percentage.toFixed(2);
console.log(fixed);
If the number doesn't have any decimal places, it gets padded with zeros.
If the number doesn’t have any decimal places, it gets padded with zeros.

Calculate Percentage increase or decrease between Two Numbers

To calculate percentage increase:

  1. Subtract the final value from the starting value.
  2. Divide that amount by the absolute value of the starting value.
  3. Multiply by 100 to get the percent increase.
  4. If the percentage is negative, it means there was a decrease and not an increase.
  5. Formula to calculate percentage increase or decrease:

    Percentage Increase = [ (numberOne – numberTwo) / numberTwo ] × 100

Let’s implement this formula in JavaScript

function getPercentageIncrease(numberOne, numberTwo) {
  return ((numberOne - numberTwo) / numberTwo) * 100;
}

console.log(getPercentageIncrease(50, 30));

console.log(getPercentageIncrease(50, 100));
Implementation of Percentage Increase formula in JavaScript.
Implementation of Percentage Increase formula in JavaScript.

This code defines a function that calculates the percentage increase between two numbers. The function takes two arguments, numberOne and numberTwo, and returns the percentage increase from numberTwo to numberOne.

The first example demonstrates that the percentage increase from 30 to 50 is 66.6666…%.

The second example shows that the percentage decrease from 100 to 50 is – 50%.

How to Calculate Percentage between Two Numbers in JavaScript.