Glance: Arrays in JavaScript

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.

Arrays in JavaScript

In order to create an array in JavaScript, a new Array object must be declared. There are two ways you can declare arrays in JavaScript, which are as follows:

Way 1:

var weekdays = new Array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");

Way 2:

var weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];

In Way 1, the new Array object has been declared explicitly.

In Way 2, the square brackets ([]) are used to indicate to the JavaScript interpreter to consider this sequence of strings as an array.

Empty Array in JavaScript

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.

Array with Size in JavaScript

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.

Length Of Array in JavaScript

You can find the length of Array in JavaScript by writing the following snippet of JavaScript code:

var tempArray = new Array("One", "Two", "Three");
// tempArray.length -> 3

tempArray.length will return 3, as there are three elements in the tempArray

Multi-Dimensional Arrays in JavaScript

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:

enter image description here 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.


In this article, we have discussed what an Array is, and how to make an Array in JavaScript. We have also learnt one-dimensional and multi-dimensional arrays. I have touched the surface of Arrays in this articles. There is much more to learn, and I shall discuss more in-depth in the future articles.



Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?