tutorialstonight logo
A Platform To Learn To Code

Star Pattern in javascript



What is the star pattern?

Star pattern is a series of * (can be another character too) which creates a certain pattern or geometrical shape such as square, pyramid, triangle, etc. These patterns are created using some nested controlled loops.

These pattern programs are used to practice and boost programming skills and also asked to create in programming interviews.

Star Pattern In Javascript

Star Pattern Programs In Javascript

Here we have discussed 10 different star patterns in detail with their programs in javascript.

  1. Square star pattern
  2. Right triangle star pattern
  3. Left triangle star pattern
  4. Downward triangle pattern
  5. Pyramid star pattern
  6. Reversed pyramid star pattern
  7. Diamond star pattern
  8. Hourglass star pattern
  9. Right Pascal star pattern
  10. Left Pascal star pattern

1. Square pattern

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

To create square pattern run 2 nested for loop. Run each loop for 'N' number of times, where 'N' is number of rows in the square, i.e for(let i = 0;i < N;i++).

Run first for loop for 'N' times, inside this loop run another for loop which runs for another 'N' times and print/adds a star to the string. After completing internal loop add a new line (\n) to the string.

Example

Try It

2. Right Triangle Star Pattern

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

To create right triangle star pattern again run 2 nested for loop. You can observe from the above-shown pattern that we need to run an external loop for 'N' time and internal loop iterations increase from 1 to 'N'. So the number of time the internal loop will run is number of times the external loop has run (i in below example).

Example

Try It

3. Left Triangle Star Pattern

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

To create left triangle star pattern again run nested for loop.

You can see in the above pattern there are 4 spaces and 1 star in the first line, 3 space and 2 stars in 2nd line and so on. So we need to run 2 loops one for printing spaces and other to print star.

Create 2 loops inside the external loop, one print spaces for (number of remained lines to print) number of times and other print stars for ('N' - number of remained lines to print) number of times.

Example

Try It

4. Downward Triangle Star Pattern

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

To create downward triangle star pattern use a nested loop. From the above-shown pattern, you can see the number of stars decreases from 'N' to 1.

Run a for loop inside external loop whose number of iterations decreases form 'N' to 1.

Example

Try It

5. Pyramid Star Pattern

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

Pyramid star pattern is famous star pattern, you can see shape of the pattern above.

It uses 2 loops inside external loop one for printing spaces and other to print star. First loop prints spaces for the number of lines external loop remain to iterate and second loop print stars.

Example

Try It

6. Reversed pyramid star pattern

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

Reversed pyramid star pattern is pyramid pattern upside-down.

It uses 2 loops inside external loop one for printing spaces and other to print star. First loop prints spaces and other loop print stars. Here is the code of reverse pyramid star pattern.

Example

Try It

7. Diamond star pattern

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

Diamond star pattern is combination of pyramid and reverse pyramid star pattern.

Observe in the above pattern it is a pyramid and a reverse pyramid together. Use the above concepts to create a diamond shape.

Example

Try It

8. Hourglass star pattern

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

Hourglass star pattern is also made up of pyramid and reverse pyramid star pattern.

Observe in the above pattern it is a reverse pyramid and a pyramid together. Use the above concepts to create a diamond shape.

Example

Try It

9. Right pascal star pattern

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

Right pascal star pattern is created using 2 nested loops.

You can observe in the above pattern it is nothing but right triangle star pattern and reversed triangle star pattern together. Here is the code for the right pascal star pattern.

Example

Try It

10. Left pascal star pattern

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

Left pascal star pattern is also created using 2 nested loops.

It is same as right pascal star pattern just mirrored. Here is the code for the right pascal star pattern.

Example

Try It