Increasing Row-Start Number Triangle in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
i + j - 1 formula

What You’ll Learn

How to print a triangle where each row starts at its row number: row 1 prints 1, row 2 prints 2 3, row 3 prints 3 4 5, and so on.

This pattern is a simple way to practice nested loops plus a tiny arithmetic expression (i + j - 1).

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
1

Complete JavaScript Program

Row i prints i values. The value printed is i + j - 1, which starts at i when j = 1.

JavaScript
const rows = 5;

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

🧠 How It Works

1

Set the number of rows

const rows = 5; sets the height of the triangle.

Setup
2

Outer loop selects the row

i goes from 1..rows. Row i will print i numbers.

Rows
3

Inner loop prints columns 1..i

j runs from 1..i so the row length grows by one each line.

Columns
4

Compute the printed value

The expression i + j - 1 starts at i and increases by 1 for each column.

Math
=

Row starts at i

Row 4 starts at 4 and prints 4 values: 4, 5, 6, 7.

2

Variation — Browser (document.write) Version

Print the triangle in an HTML page using document.write:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var i, j;
for (i = 1; i <= 5; i++) {
  for (j = 1; j <= i; j++)
    document.write(i + j - 1 + " ");
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Change the starting value by adding an offset: (offset + i + j - 1)
  • Use padStart to align columns for larger values
  • Print the triangle in reverse by looping i from rows down to 1
  • Replace the formula with i * j for multiplication patterns

Avoid

  • Leaving trailing spaces in console output when comparing exact output
  • Printing without separators (numbers may run together)
  • Hard-coding row counts if you want a reusable pattern function

Key Takeaways

1

Row i prints exactly i values (triangle growth).

2

The formula i + j - 1 makes each row start at i.

3

Changing the expression changes the entire pattern.

4

Nested loops are the foundation for most pattern-printing programs.

❓ Frequently Asked Questions

Row 5 prints i + j - 1 for j = 1..5, resulting in 5, 6, 7, 8, 9.
Print a fixed value instead of using j in the expression (for example, just print i).
Yes. Use (j - 1) as the printed value for each row.
Add alignment and spacing logic (leading spaces per row) similar to centered pyramid patterns.

Explore More JavaScript Number Patterns!

Small formulas like i + j - 1 can generate surprisingly rich patterns.

All Number Patterns →
Did you know?

Patterns that use i + j are closely related to “diagonals” in a grid. Each diagonal shares the same sum, which is why these patterns look like shifted sequences.

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