In this first example, an array is created using the new constructor, then each of the members of the Array are assigned variables through an index. In this example, the Array is constructed and the values are assigned in one statement. You can create an Array with a specific number of members, without having to assign the values. While this is not recommended, the following example creates an Array with 10 empty members. In JavaScript, the use of the new constructor is not required when creating arrays, as shown in this example. You do not need to specify how many members the Array will contain. JavaScript will automatically increase the size of the Array as needed. Creating an Array with brackets instead of with the new constructor avoids confusion where you want to initialize only one integer.

How to Use Arrays

Once you assign values to an Array, you can access the members of the array and modify the values as well. Keep in mind that the first index in an Array is [0], so if you want to access the third member of the Array, you can do so as follows: If you want to modify the value of the first member of the Array you would do so as follows:

Array Object Length Property

All Array objects have a length property. This property contains the number of elements in the array. The length property is convenient for loops. You can use a loop and counter to easily compare against the number of members or even navigate through its members by looping through the index.

Array Object Methods

Just as other JavaScript objects, arrays have methods you can use. Here is a listing of the Array object methods.

concat()

The concat method is used to join multiple arrays. This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

indexOf()

The indexOf method will search the array until it matches your search criteria. It will then return the index where the item was found.

join()

The join method will output your Array as a string with a delimiter of your choice.

lastIndexOf()

The lastIndexOf is similar to indexOf, except it searches from last to first.

pop()

The pop method removes the last element of an array. This method changes the length of an array.

push()

The push method adds new items to the end of an array. The new item(s) will be added at the end of the array. This method changes the length of the array.

reverse()

The reverse method reverses the order of the members of the array.

shift()

The shift method removes the first item of an array. This method changes the length of an array.

slice()

The slice method returns the selected members in an array, as a new array object. The slice method requires that you specify the start and end index. However, the end index is not included in the new array. The original array will not be changed. Keep in mind that the array index begins at zero.

sort()

The sort method sorts the items of an array. The sort order can be either alphabetic or numeric, and either ascending or descending. The default sort order is alphabetic and ascending. You can create a function and pass the sort order as a parameter in the method as well.

splice()

The splice method adds or removes items to and from an array. This method changes the original array. In the following example, we first use the splice method with no additional items to be added. We are simply telling JavaScript to move to index[2] and remove two items. The next splice method moves to index[1], removes zero items, then adds additional strings to the array.

toString()

The toString method converts an array into a string and returns the result. The returned string will separate the elements in the array with commas.

unshift()

The unshift method adds new items to the beginning of an array. This method changes the length of an array.

valueOf()

The valueOf method returns the primitive value of an array. Always keep in mind that JavaScript is case sensitive.