Arrays in Javascript



Arrays are useful way to store information and is a prominent data structure in all programming languages.
There are many ways to create javascript arrays as in the examples below. By default the javascript arrays start from index 0.

Example 1: Javscript Arrays

    rollnumbers = new Array(11, 22,33,44,55); 

Example 2: Javscript Arrays

	<script>
	        rollnumbers = new Array(6);
	        rollnumbers[0] = "11"
	        rollnumbers[1] = "22"
	        rollnumbers[2] = "33"
	        rollnumbers[3] = "44"
	        rollnumbers[4] = "55"
	        rollnumbers[5] = "66"
	</script>

Example 3: Javscript Arrays

	<script>
	        rollnumbers = ["11", "22", "33", "44", "55", "66"];
	</script>
 

Example 4: Printing Javscript Arrays

To print the value in the javascript array, you can access the value by the integer key. Example:

	<script>
	       alert rollnumbers[4]; 
	</script>
 

This would generate the alert 55.

Methods Of Array Object

Many operations can be performed with Arrays.There are many pre-defined functions or methods of array object.Here is a list of them.

Method Operation
Concat It adds one array to another array.
Pop It removes one element of the array.
Push It adds one element in the array.
Slice It slices a section of the array.
Splice It adds one element to the specified position.
Sort It converts array into either ascending or descending order.
toString It converts an array to string.




Nice one, would like to see this also cover JSON, which are like arrays.