Ascending Number Triangle in JavaScript

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

What You’ll Learn

How to print an ascending number triangle in JavaScript using nested for loops. Each row starts at 1 and grows by one number.

This pattern is ideal for understanding how the outer loop controls row length and the inner loop prints values from 1 to the current row.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
12
123
1234
12345
1

Complete JavaScript Program

The outer loop increases row size from 1 to rows. The inner loop prints numbers from 1 to the current row limit.

JavaScript
const rows = 5;

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

🧠 How It Works

1

Set number of rows

const rows = 5; defines the triangle height.

Setup
2

Outer loop (growing rows)

for (let i = 1; i <= rows; i++) increases row size one step at a time.

Row control
3

Inner loop (print 1..i)

for (let j = 1; j <= i; j++) appends each number up to current row limit i.

Number printing
4

Print completed row

console.log(line); outputs one row per iteration.

Line break
=

Ascending number triangle

Total printed values are 1+2+…+n = n(n+1)/2, giving time complexity 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 = 1; j <= i; j++) {
    document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Validate input and handle non-positive rows safely
  • Add spaces between values for clearer output
  • Pad leading spaces to center the triangle shape
  • Use user input to make the pattern dynamic
  • Convert this to a reusable function for practice

Avoid

  • Skipping row breaks after the inner loop
  • Mixing up loop variables i and j
  • Using wrong loop limits that overprint or underprint rows
  • Relying on document.write for production UIs

Key Takeaways

1

The outer loop decides how many numbers each row contains.

2

The inner loop prints values from 1 to i.

3

Rows grow one value at a time, forming an ascending triangle.

4

This is a foundational loop pattern for many algorithm problems.

❓ Frequently Asked Questions

Because inner loop starts from j = 1 for every row and runs until j <= i.
Increase rows. For example, set rows = 6 to print up to 123456.
Yes, and it is often better. Build line for each row and print once with console.log(line).
O(n²) with total printed values n(n+1)/2.

Explore More JavaScript Number Patterns!

Keep building loop confidence by practicing more increasing and decreasing pattern programs.

All Number Patterns →
Did you know?

The total count of numbers printed after n rows is a triangular number, which appears in many counting and combinatorics 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