Here is an example before going in to deep: Suppose that you have a list of country names. If you have to update or store data, manually you will update data one by one if you don’t have knowledge of array. If the list is containing 100, 200 or 20000 items, then it becomes hard to handle this. At this point concept of array will be used. Arrays are one way of keeping a program more proper. They allow you to do lots of operation that are difficult without them. Arrays are usually a group of the same variable type that use an index number to distinguish them from each other
- Values are stored into an array by using the array name and by stating the index in the array you wish to store the value in brackets. Example: name_of_array = "India is A Secular Country";
- Values in an array are accessed by the array name and index of the value. Example: name_of_array;.
var country_name=new Array(); country_name="india"; country_name="pakistaan"; country_name="Shree lanka"; country_name="Bangladesh"
var country_name=new Array("india","pakistaan","shree lanka","bangladesh");
var country_name=["india","pakistaan","shree lanka","bangladesh"];
Listing 1: Creating an Array and using it
Figure 1: Above figure is output of simple array script in which stored item in array will pop up after clicking on the button.
Listing 2: Script to Sort an Array in Alphabetical Order
Figure 2: Above figure is output of sorting script in which original array contains (0) (1) (2) (3) (4) sequence and after sorting alphabetically sorted array is in sequences of (0) (2) (4) (1) (3)
Concatenated_Array = oneArray.concat( secondArray, thirdArray, ...);
Listing 3: Array concatenation script
The example takes two array object firstArray and secondArray.
Figure 3: Above figure is output of concatenating array string in which concat () method takes two array value and adds in a final array. The example takes two array object "firstArray" and "secondArray". firstArray contains four elements and secondArray contains two elements and values are merged in finalArray.