Listing 1: Representing the Array declaration
var empty_arr = ; // An empty array var prime_num = [2, 3, 17, 11,13]; // An array with 5 prime numbers var mix_val = [ 1.1, true, "a", ]; // three different types and a trailing comma
An array can hold not only constant values but arbitrary expressions also:
var b = 1024; var table = [b, b+1, b+2, b+3];
If a value is omitted from an array, then the omitted value is given a value undefined:
var arr1 = [1,,3]; // the middle one undefined. var arr2 = [,,]; // both element are undefined.
There is another way to create an array, use the Array() constructor. This constructor can be invoked in here ways:
- Use the Array() constructor without any argument:
- Pass an argument which specifies the length of the array:
- You can specify either two or more array elements or single non-numeric element for the array:
var b=new Array();
An empty array with no element is created when this method is used.
Var b=new Array(10);
This method will create an array of specified length. This method is used when user knows in advance that how many elements are going to be needed. There is no value stored in array.
var c = new Array(6,7,8,9,10, "testing…….");
In this method, the constructor arguments are the elements of the array c.
Reading and Writing Array Elements
An  operator is used to access the elements of an array. The brackets should be supplied with a non-negative integer value. This syntax can be used for both read and write operations on an array. Here, some examples are provided below:
Listing 2: Representing read and write operation on an array
var a = ["alok"]; // create a one-element array var value = a; // Read element 0 a = 4; // Write element 1 i = 2; a[i] = 6; // Write element 2 a[i + 1] = "hello"; // Write element 3 a[a[i]] = a; // Read elements 0 and 2, write element 3
If an array has elements having non-contiguous indexes starting at zero, this type of array is called a sparse array. In arrays, the length property is generally represents the number of elements in array. In the case of sparse array, the value of length property is greater than the numbers of elements present in array. The implementation of an sparse array needs a slower, more memory efficient way than regular arrays. We can create an sparse array by assigning an array index larger than the current array length or we can use Array() constructor to create them:
Listing 3: Representing the examples of Sparse array
c = new Array(6); // Array is empty but a.length is 6. c = ; // Array is empty and length is zero. c = 0; // A single value is assigned but set the length to 1001
Length of an Array
Listing 4: Representing the examples of Length property in array
.length // it returns a zero, array has no element ['d','e','f'].length // return 3 because highest index is 2
In the case of an sparse array, the length property always return a value one more than its highest index number or we can say that it is impossible to have an element with greater or equal index to its length. But if in any case, if there is an element whose index is greater than or equal to the length, then that element will be deleted from that sparse array:
Listing 5: Representing the other examples of Length property in array
b = [6,7,8,9,10]; // 5-element array is created. b.length = 3; // array has only [6,7,8] elements, last 2 have be deleted. b.length = 0; // All elements have been deleted, array is empty. b.length = 5; // Array length is set to 5 but have no element.
push() and delete() Elements in an Array
Simplest method of inserting values in arrays is just assign values to new indexes:
Listing 6: Representing the examples of push() and delete() Elements in array
Example =  // Create an empty element Example = "alok"; // And add elements to it. Example = "one";
A push() method is used to insert new elements at the end of the array:
b = ; // Create an empty array b.push("zero") // A value is inserted at the end of the array b.push("one", "two") // two more values are inserted
push() method is same as assigning the value to the a[a.length]. If you want to insert an element at the beginning of an array, then an unshift() method can be used, which shifts the existing elements to higher index.
To delete an array element, a delete operator is used:
d = [1,2,3]; delete d; // a now has no element at index 1 1 in d // false: no array index 1 is defined d.length // 3: delete does not affect array length
When use are using delete() method, you should keep one thing in your mind, deleting an element does not change the length property. It means no higher indexed value is shifted down to fill-in the gap created by the deleted element. Deleting is similar to assign undefined to that element. I this case, the array will become a sparse array.
A user can also delete elements from the end of an array by only setting the length property to the new desired length. Arrays have a pop() method also which is used to remove elements from the end and also returns the value of the deleted item. A shift() method is also available which shifts all elements to one lower than their current index.
Array reverse() method
The Array.reverse() method is used to reverse the order of the elements of an array and return the values in return order. This method does not create an array of rearranged elements, but it actually rearranged the existing order of that array and then returns the values. In the following example, a reverse() and join() method is used to produce the string “10,9,8”:
Listing 7: Representing the examples of reverse() method of array
var a = [8,9,10]; a.reverse().join() // => a is now [10,9,8] and returns “10,9,8”