Print Star Pattern in JavaScript


Prev Tutorial Next Tutorial

Print Star Pattern in JavaScript

Using JavaScript we can easily write any programming code, Here we write one of the most popular program Print Star Pattern in JavaScript. To perform this task we need for loop. In JavaScript we use document.write() function to print * on screen.

print star pattern in javascript

Print Triangle of Stars in JavaScript

<html>
<head>
<script type="text/javascript">

  var i, j;
  //outer loop
  for(i=1; i <= 5; i++)
   {
   //inner loop
    for(j=1; j<=i; j++)
   {
     document.write('*');
    }
     document.write('<br/>');
   }

</script>
</head>
<body>
</body>
</html>

Output

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

Print Triangle of Stars in JavaScript

<html>
<head>
<script type="text/javascript">

  var i, j;
  //outer loop
  for(i=5;i>=1;i--)
   {
   //inner loop
    for(j=1;j<=i;j++)
   {
     document.write('*');
    }
     document.write('<br/>');
   }

</script>
</head>
<body>
</body>
</html>

Output

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

Prev Tutorial Next Tutorial

Advertisements





Download Projects