Arrays

A JavaScript array is an indexed list of values, with index of the first value starting with 0.

Creating an Array

javascript arrays

The easiest way to create an array consisting of N elements is as

					
					var arr = [element1, element2, ..., elementN]; 
					
				

where arr is some variable to which it is assigned. This way of creating an array is known as array literal notation. Here is an example of an array consisting of four elements initialized using the literal notation

					
					var os = ["Debian", "Fedora", "openSUSE", "Ubuntu"];
					
				

We can create the equivalent of the above array using the built-in Array constructor and the new keyword, passing the to-be elements of the array as its arguments.

					
					var os = new Array("Debian", "Fedora", "openSUSE", "Ubuntu");
					
				

Alternatively, a single positive integer (or zero) can be passed to the constructor, which then becomes the length of the array. Elements can then be added to it.

					
					var os = new Array(4);
					os[0] = 'Debian';
					os[1] = 'Fedora';
					os[2] = 'openSUSE';
					os[3] = 'Ubuntu';
					
				

An empty array is just an empty pair of square brackets with no element in it.

					
					var os = [];
					
				

The length Property

The Array length property gives the next integer greater than the highest index in an array. The above array consists of four elements - Debian, Fedora, openSUSE, Ubuntu - with the highest index being 3. Therefore, the below line of code

					
					os.length; // 4
					
				

will return 4.

Accessing Array Elements

Array indexes start with 0. The first element of an array is accessed by putting its index 0 in square brackets. In the above example array, the first element is accessed as

					
					os[0];  // Debian
					
				

and it returns the value Debian. os[1] will return the second element Fedora. If, however, the index has a value greater than the length of the array (number of elements in an array), it will return undefined. For example, os[7] will return undefined as there is no value assigned to element with index 7.

The last element of an array can be accessed as

					
					os[os.length - 1]; // Ubuntu
					
				

Adding/Removing Array Elements

The Array methods push() and pop() are used for adding and removing elements to and from an array respectively. The push() method appends one or more elements to the end of an array. Here we add another element Gentoo to the end of an array

					
					var os = new Array("Debian", "Fedora", "openSUSE", "Ubuntu");
					os.push('Gentoo');
					
				

and the resulting new array becomes

					
					["Debian", "Fedora", "openSUSE", "Ubuntu", "Gentoo"];
					
				

If we apply the pop() method on the above modified array

					
					os.pop();
					
				

it will remove the last element Gentoo from it and the resulting array would again be

					
					["Debian", "Fedora", "openSUSE", "Ubuntu"];
					
				

Looping Through an Array

The JavaScript Array forEach() method executes a callback function for each element in an array. The callback function takes three parameters: the value of the array element, its numeric index and the array containing the element itself. The below example illustrates the use of the forEach() method

					
					var os = new Array("Debian", "Fedora", "openSUSE", "Ubuntu");

					os.forEach(function(value,index,arr){
						console.log("Value: " + value + ", Index: " + index);
					});
					
				

and it prints the following messages into the console

					
					Value: Debian, Index: 0
					Value: Fedora, Index: 1
					Value: openSUSE, Index: 2
					Value: Ubuntu, Index: 3
					
				

More details on the JavaScript Array forEach() method can be found under the following link: js-array-foreach.html