Arrays

An array contains a set of data represented by a single variable. An array is a JavaScript object.Since arrays are objects, you create them using the new keyword and JavaScript's Array() constructor object. The Array object constructor provides support for creation of arrays of any data type. Each piece of data stored in an array is called an element. To find the number of elements in an array object you can use the length property


Each of these is a valid way to create an array object
arrayObj = new Array()
arrayObj = new Array(arrayLength)
arrayObj = new Array(element0, element1, ..., elementN)

Create an array using: arrayObj = new Array(arrayLength)

If the arrayLength is given, then this is the initial length of the array. The arrayLength must be an unsigned 32-bit integer. If the value is a number, but is less than zero or is not an integer, a run-time error occurs. As arrays are zero-based, created elements will have indexes from zero to (size -1).

For example,

  1. create an array with three elements. Assign each element a value. After an array is created, the individual elements of the array can be accessed using [ ] notation. The first element in the array is musicTypes[0]. You assign a value to an element in the array just like any variable and access an element just like any other variable.

    	musicTypes = new Array(3);
    	musicTypes[0] = "R&B";		//first element
    	musicTypes[1] = "Blues";		//second element
    	musicTypes[2] = "Jazz";		//third element
    	
    	document.writeln(musicTypes[0]); 	//prints R&B
    	document.writeln(musicTypes[1]); 	//prints Blues
    	document.writeln(musicTypes[2]); 	//prints Jazz
    	document.writeln(musicTypes.length); //prints 3
    	
  2. create an array using: arrayObj = new Array(element0, element1, ..., elementN)

    If the elementN is given, , the array is initialized with the specified values as its elements, and the array's length property is set to the number of arguments. For example the musicTypes array could have been created:

    	musicTypes = new Array("R&B", "Blues", "Jazz");
    	
    	document.writeln(musicTypes[0]); 	//prints R&B
    	document.writeln(musicTypes[1]); 	//prints Blues
    	document.writeln(musicTypes[2]); 	//prints Jazz
    	document.writeln(musicTypes.length); //prints 3
    	

    click here to see the demo.

  3. If a single value is passed to the Array constructor, and it is not a number, the length property is set to 1, and the value of the only element becomes the single, passed-in argument. For example, if only one parameter or argument was passed to the Array constructor:

    	musicTypes = new Array("R&B");
    	
    	document.writeln(musicTypes[0]); 	//prints R&B
    	document.writeln(musicTypes.length); //prints 1
    	
  4. Dynamic array size

    When you create a new array with the constructor Array() object, the number of elements is optional. If you do not provide the number of elements the size of the array is dynamic. As the elements in an array do not have to be contiguous, the length property is not necessarily the number of elements in the array. For example, in the following array definition, character_array.length contains 7, not 2:

    	var character_array = new Array( );
    	character_array[0] = "Goofy";  		//element 1
    	character_array[3] = "Pluto";  			//element 4
    	character_array[6] = "Mickey"; 		//element 7
    	document.writeln(character_array.length);	//prints 7
    	

    Notice, any new elements created have the value undefined .

    click here to see the demo.

  5. Debugging an array

    If you notice how each element in the array is assigned and how to display each one, you will notice that an index is used. You also can find out how many elements are in the array by using the lengh property. With this information we can write a for loop to easily display each element in the array.

    	musicTypes[0] = "R&B";		//first element
    	musicTypes[1] = "Blues";	//second element
    	musicTypes[2] = "Jazz";		//third element
    	
    	document.writeln(musicTypes[0]); 	//prints R&B
    	document.writeln(musicTypes[1]); 	//prints Blues
    	document.writeln(musicTypes[2]); 	//prints Jazz
    	document.writeln(musicTypes.length); //prints 3
    	
    	for (i=0;i<musicTypes.length;i++)
    		document.write(i + " : " + musicTypes[i]);
    	
    	

    click here to see the demo.