JavaScript Program to Print Pascal Triangle
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j], then printing each row with console.log().Examples
How to Think About It
Algorithm
Code
function printPascalTriangle(n) { const triangle = []; for (let i = 0; i < n; i++) { triangle[i] = []; triangle[i][0] = 1; for (let j = 1; j < i; j++) { triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]; } if (i > 0) triangle[i][i] = 1; } for (let row of triangle) { console.log(row.join(' ')); } } printPascalTriangle(5);
Dry Run
Let's trace printing Pascal's Triangle with 3 rows through the code
Initialize triangle array
triangle = []
First row (i=0)
triangle[0] = [1]
Second row (i=1)
triangle[1][0] = 1; triangle[1][1] = 1; triangle[1] = [1, 1]
Third row (i=2)
triangle[2][0] = 1; triangle[2][1] = triangle[1][0] + triangle[1][1] = 1 + 1 = 2; triangle[2][2] = 1; triangle[2] = [1, 2, 1]
Print rows
Print '1', then '1 1', then '1 2 1'
| Row | Values |
|---|---|
| 0 | [1] |
| 1 | [1, 1] |
| 2 | [1, 2, 1] |
Why This Works
Step 1: Start each row with 1
The first number in every row of Pascal's Triangle is always 1 because it represents the edge.
Step 2: Calculate inner values
Each inner number is the sum of the two numbers above it from the previous row using triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j].
Step 3: End each row with 1
The last number in every row (except the first) is also 1, completing the symmetrical shape.
Step 4: Print the triangle
Join each row's numbers with spaces and print them line by line to display the triangle.
Alternative Approaches
function pascalRow(n) { if (n === 0) return [1]; const prev = pascalRow(n - 1); const row = [1]; for (let i = 1; i < n; i++) { row[i] = prev[i - 1] + prev[i]; } row.push(1); return row; } function printPascalTriangleRec(n) { for (let i = 0; i < n; i++) { console.log(pascalRow(i).join(' ')); } } printPascalTriangleRec(5);
function factorial(num) { let fact = 1; for (let i = 2; i <= num; i++) fact *= i; return fact; } function binomialCoeff(n, k) { return factorial(n) / (factorial(k) * factorial(n - k)); } function printPascalTriangleBinomial(n) { for (let i = 0; i < n; i++) { let row = []; for (let j = 0; j <= i; j++) { row.push(binomialCoeff(i, j)); } console.log(row.join(' ')); } } printPascalTriangleBinomial(5);
Complexity: O(n^2) time, O(n^2) space
Time Complexity
The program uses nested loops: the outer loop runs n times and the inner loop runs up to n times, resulting in O(n^2) time.
Space Complexity
It stores all rows in a 2D array, which requires O(n^2) space for n rows.
Which Approach is Fastest?
The iterative approach is faster and uses less overhead than recursion or factorial-based methods, especially for larger n.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative (main) | O(n^2) | O(n^2) | Balanced speed and memory for most cases |
| Recursion | O(n^2) | O(n^2) | Clear logic but slower due to repeated calls |
| Binomial Coefficients | O(n^3) | O(n) | Mathematical approach, slower for large n due to factorial |