JavaScript program to print Pyramid with Stars


Following program shows you how to print pyramid with stars using for loop

let n = 5; // you can take input from prompt or change the value
let string = "";
// External loop
for (let i = 1; i <= n; i++) {
  // printing spaces
  for (let j = 1; j <= n - i; j++) {
    string += " ";
  }
  // printing star
  for (let k = 0; k < 2 * i - 1; k++) {
    string += "*";
  }
  string += "\n";
}
console.log(string);

Output:

    *
   ***
  *****
 *******
*********

Try it Online here https://onecompiler.com/javascript/3x5qurk6u