Reverse-Build Number Pattern in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Modulo + while loop

What You’ll Learn

How to build the reverse of a number one digit at a time and print each intermediate result.

This pattern is essentially the number-reversal algorithm, but we print at each step to form a pattern.

⭐ Pattern Output

For num = 86523, the output looks like this:

Output
3
32
325
3256
32568
1

Complete JavaScript Program

Take the last digit with % 10, append it to reverse, print, then drop the digit from num.

JavaScript
let num = 86523;
let reverse = 0;

while (num !== 0) {
  reverse = reverse * 10 + (num % 10);
  console.log(reverse);
  num = Math.floor(num / 10);
}

🧠 How It Works

1

Initialize num and reverse

num = 86523, reverse = 0.

Start
2

Extract the last digit

digit = num % 10 returns the last digit.

Modulo
3

Append digit to reverse

reverse = reverse * 10 + digit appends the new digit.

Build
4

Print and reduce num

Print reverse, then remove the last digit using Math.floor(num / 10).

Loop
=

Pattern emerges

Each step adds one more digit to the reversed number until the full reverse is formed.

2

Variation — Browser (document.write) Version

Print the intermediate reverse values in the browser:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var num = 86523;
var reverse = 0;
while (num != 0) {
  reverse = reverse * 10 + (num % 10);
  document.write(reverse + "<br>");
  num = Math.floor(num / 10);
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Change num and observe how the reverse grows each step
  • Use Math.trunc instead of Math.floor if you plan to handle negative numbers
  • Print both num and reverse each iteration for debugging
  • After the loop, print the final reverse on a separate line

Avoid

  • Using document.write in production apps (fine for demos)
  • Forgetting to update num (would cause an infinite loop)
  • Using floating-point values for num

Key Takeaways

1

num % 10 gives the last digit.

2

reverse = reverse * 10 + digit appends digits to build the reverse.

3

Math.floor(num / 10) removes the last digit from the original number.

4

Printing each intermediate reverse creates the pattern.

❓ Frequently Asked Questions

Yes—this is the standard reverse-number algorithm, but we print reverse at every step to form a pattern.
Because the last digit of 86523 is 3. We append it to reverse first, then print.
Yes—use strings and take prefixes (e.g., s.slice(0, i)) to grow from the left.
Trailing zeros become leading zeros in the reverse, which are not shown in numeric output. Use strings if you need to preserve them.

Want More Digit Patterns?

Try printing the number as it shrinks (Program 60) and compare it with this growing reverse output.

All Number Patterns →
Did you know?

The modulo operator % is one of the most common tools for digit extraction problems.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful