Shifted Number Triangle in JavaScript

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a shifted number triangle in JavaScript using nested for loops. Each row starts from the current row number and prints up to rows.

This pattern helps you understand loops where the inner loop start value changes on each row: first 1..rows, then 2..rows, then 3..rows, and so on.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
12345
2345
345
45
5
1

Complete JavaScript Program

The outer loop sets the row start value. The inner loop prints numbers from that start up to rows.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = i; j <= rows; j++) {
    line += j;
  }
  console.log(line);
}

🧠 How It Works

1

Choose the row count

const rows = 5; defines the highest number and total rows.

Setup
2

Outer loop (row start)

for (let i = 1; i <= rows; i++) increases the start value on each row.

Row control
3

Inner loop (print i..rows)

for (let j = i; j <= rows; j++) appends values from the current start i to rows.

Number printing
4

Print each built row

console.log(line); outputs each completed row on a new line.

Line break
=

Shifted number triangle

The total printed values are n + (n-1) + ... + 1 = n(n+1)/2, so complexity remains O(n²).

2

Variation — Browser (document.write) Version

Print the same pattern directly in an HTML page using document.write:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
const rows = 5;

for (let i = 1; i <= rows; i++) {
  for (let j = i; j <= rows; j++) {
    document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing
  • Add spaces between digits for readability (line += j + " ";)
  • Change to descending rows by reversing the outer loop direction
  • Pad left spaces to align pattern to the right
  • Replace numbers with letters for alphabet variants

Avoid

  • Forgetting <br> (or newline) after each row
  • Starting inner loop from a fixed value when pattern needs shifting start
  • Using non-numeric input without conversion/validation
  • Overusing document.write in production code; prefer DOM updates

Key Takeaways

1

The outer loop controls the row start value from 1 to rows.

2

The inner loop prints from i to rows, creating a shrinking width.

3

Total printed values are triangular in count: n(n+1)/2.

4

The same nested-loop approach applies to star patterns and alphabet patterns.

❓ Frequently Asked Questions

Because the outer loop variable i becomes the starting value for each row, so rows start at 1, 2, 3, and so on.
Use for (let i = 1; i <= rows; i++) and inside it for (let j = i; j <= rows; j++). Print each j and add a newline after the inner loop.
Yes. Build each row in a string and print with console.log(line). This is cleaner and preferred for JavaScript practice.
O(n²) for n rows, because total printed values are n(n+1)/2.

Explore More JavaScript Number Patterns!

Practice more nested-loop patterns and build stronger problem-solving skills with each variation.

All Number Patterns →
Did you know?

Even though each row starts differently, the total amount of printed numbers is still a triangular number: 1+2+…+n = n(n+1)/2.

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