How to Use Star Looping in JavaScript?

Estimated read time 1 min read

A “star loop” is commonly used to generate patterns of asterisks (star symbols) in JavaScript. Here’s how you can use a loop to generate a pattern of asterisks:

for (let i = 1; i <= 5; i++) {
  let row = "";
  for (let j = 1; j <= i; j++) {
    row += "*";
  }
  console.log(row);
}

This code uses two nested for loops to create a pattern of asterisks. The outer loop (i) controls the number of rows, while the inner loop (j) controls the number of asterisks in each row.

The row variable is used to build up each row of asterisks, and it is reset to an empty string for each new row. The console.log function is used to print each row of asterisks to the console.

This code will generate the following output:

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

You can modify this code to generate different patterns of asterisks by changing the values used in the for loops, or by modifying the logic used to build up each row of asterisks.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply