JavaScript array for loop

In this Tutorial we want to understand you how an array for loop is executed in a JavaScript.

JavaScript array for loop

In this Tutorial we want to understand you how an array for loop is executed in a JavaScript.

JavaScript array for loop

JavaScript array for loop

     

In this Tutorial we want to understand you how an array for loop is executed in a JavaScript. The code import the following packages-

1)java.util   -  This Package contains the collections framework, legacy collection classes, event model, date and time facilities and internationalization..

2)java.text  - This Package contains the classes and interface for handling text,message,date and numbers.

Method Used - 

1)Format formatter  = new SimpleDateFormat("MMMM")  - This method include a date format ,this enables formatting i.e., date -> text, parsing text -> date, and normalization. The date is represented as a date object or the milliseconds since January 1, 1970, 00:00:00 GMT.

2)calendar.getInstance ( )  -   This method return you default time zone and current locale.

We declare a variable string array name month, that is used to instantiate an string object with initial size to hold 12 element. The for loop execute and run the script till the length i is less than length of string element.

3)Calendar.set(calendar.Month,i) - This method provides you to set you the calendar month.

4)calendar.gettime( )  -This method provides you to get  the calendar current time.

5)monthname.length ( )-This method return you the length of the month.

The var  month instantiate an array object that hold the size of month. The for loop run and execute the script till the length of i is less than  month. The document. write print the list of month in a year.

 

 

ArrayForLoop.jsp

<html>
  <head>
  <title>Array Elements</title>
  
  <%page import="java.util.*" %>
  <%page import="java.text.*" %>
  <%
  Format formatter = new SimpleDateFormat("MMMM");
  Calendar calendar = Calendar.getInstance();
  String[] monthName = new String[12];

  for (int i = 0; i < 12; i++) {
  calendar.set(Calendar.MONTH, i);
  monthName[i= formatter.format(calendar.getTime());
  }

  int month = monthName.length;
  %>
  </head>
  
  <body>
  <script>  
  var month = new Array(<%=month%>);

  <%for (int i = 0; i < month; i++) {%>
  month[<%=i%>"<%=monthName[i]%>";

  <%}%>
  document.write("<h2>Elements of the array</h2>");
  fori = 0; i < month.length; i++){
  document.write("<b>"+month[i]+"</b><br>");
  }
  </script> 
  </body>
</html>

Output

Download code