Remove-Last-Digit Number Pattern in JavaScript

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
While loop + division

What You’ll Learn

How to print a number, then repeatedly remove its last digit and print again until it becomes 0.

This is a great warm-up for digit manipulation and while loops.

⭐ Pattern Output

For num = 86523, the output looks like this:

Output
86523
8652
865
86
8
1

Complete JavaScript Program

We print the current number, then update it using integer division by 10 until it becomes 0.

JavaScript
let num = 86523;

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

🧠 How It Works

1

Initialize num

num = 86523 is the starting value.

Start
2

While num is not zero

The loop continues until all digits are removed.

Loop
3

Print current num

console.log(num) prints the current value on its own line.

Output
4

Remove the last digit

Math.floor(num / 10) drops the last digit.

Truncate
=

Digit reduction pattern

Each iteration prints a shorter number until only one digit remains.

2

Variation — Browser (document.write) Version

Print the pattern in the browser using document.write:

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

💡 Tips for Enhancement

Try These

  • Change num to print different digit reduction patterns
  • Use a do-while loop so 0 prints once (if you want)
  • Try removing digits from the left using string slicing
  • Count how many steps (digits) the number has while printing

Avoid

  • Using floating-point division without Math.floor (you’ll get decimals)
  • Forgetting the loop condition (could lead to infinite loop if num never changes)
  • Using document.write in production code (fine for tutorials)

Key Takeaways

1

Dividing by 10 and flooring removes the last digit.

2

A while loop naturally repeats until the number becomes 0.

3

This prints one line per digit removed.

4

The same technique is used in many digit-processing problems.

❓ Frequently Asked Questions

You can take Math.abs(num) first, or handle the sign separately. The pattern is usually shown with positive numbers.
Use a do...while loop so the last value (0) prints once before the loop ends.
Yes—convert to a string and repeatedly remove the last character (e.g., s = s.slice(0, -1)).
Yes—Math.floor(-1.2) becomes -2. If you want digit removal for negatives, work with Math.trunc or absolute values.

Explore More JavaScript Number Patterns!

Try reversing this idea by building the number digit-by-digit instead of removing digits.

All Number Patterns →
Did you know?

Removing digits with division is used in many problems like reversing numbers, counting digits, and checking palindromes.

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