Have you ever wondered how you can group the similar things together in the programming world? If not, this article will show you how exactly you can group things together. For this purpose, in Computer Science, we use the word Array. Let us define an array first!
An array is a set of variables—strings or numbers—that are grouped together and given a single name.
An array could be used to hold a set of strings representing the names of days in the week. It can named weekdays and contain seven different strings, each representing the day of a week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.
Items in an array can be arranged in a particular order, but the good thing about arrays is that it can be manipulated any time in whatever way you want.
var weekdays = new Array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
var weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
In Way 1, the new Array object has been declared explicitly.
An empty array may be created in the following way:
var emptyArray = new Array();
The array means that it has no elements at all, but elements can be added when needed.
If we know that how many items will be there in the Array, but we do not know what those elements are, we create an array with size. This can be done in the following way:
var arrayWithSize = new Array(7);
The above Array arrayWithSize has size seven. You can fill these seven slots later in time when needed or required.
var tempArray = new Array("One", "Two", "Three"); alert(tempArray.length) // tempArray.length -> 3
tempArray.length will return 3, as there are three elements in the
The arrays explained so far are One-Dimensional Arrays. They are just list of some items. What if we want to create Table? It means we need to express that data into more than one dimension. Let us talk about multi-dimensional arrays.
Example of Multi-Dimensional Arrays:
As you can see in the figure above that we have two columns; first column contains the names of weekdays, and the second column is specifying their position in a week calendar. Now the question is how can we save both of these columns in an Array. Can we use one-dimensional array? NO! We cannot use the one-dimensional array. One way, yeah, in this particular example we can actually by saving both column's elements in alternation, like: Monday, 1, Tuesday, 2, and so on. But in most of the complicated tables, which has more than two columns, we are bound to use multi-dimensional array. Therefore, we shall use multi-dimensional array in this case.
For every row, let us define an individual array:
var mondayArray = new Array("Monday", 1); var tuesdayArray = new Array("Tuesday", 2); var wednesdayArray = new Array("Wednesday", 3); var thursdayArray = new Array("Thursday", 4);
Now that we have defined each row in terms of their respective array, it is time to make the multi-dimensional array. The array of arrays!
var weekdays = new Array(mondayArray,tuesdayArray,wednesdayArray,thursdayArray);
The above snippet of code is the example of multi-dimensional array.