An array is a variable that can hold one or more values. The following is an example of an array.
Items in an array are indexed starting with '0'. In the above example, the array items have the following index positions: elm = 0, maple = 1, ash = 2, oak = 3, cedar = 4. Note that the last item in the array has an index value one less than total number of items in the array. We can use the index positions to work with items in the array. For instance, if we wanted to print "oak" we could use the following code:
To perform an operation on all items in an array, the following structure can be a good starting point.
The following code will loop through and print all items in the 'sampleArray' array.
An array can be created using several techniques.
Another way.
Performing operations on all element of two-dimensional array can be accomplished using nested for-loops.
JavaScript has a number of built-in methods for working with arrays. In addition to returning information from an array, some of these methods change the underling array.
.concat() | Syntax: array.concat(ar1, ar2, ...) | Uses: Returns a new array consisting of the values from two or more arrays. |
| sampleArray.concat(sampleArray2) | Returns ["elm", "maple", "ash", "oak", "cedar", "willow", "pecan", "poplar"] |
|
.indexOf() | Syntax: array.indexOf(item,start) | Uses: Searches for and returns the index of a value. A start point is optional. |
| sampleArray.indexOf("oak") | Returns 3 |
|
.join() | Syntax: array.join(separator) | Uses: Creates a string including all elements from an array. |
| sampleArray.join() | Returns "elm,maple,ash,oak,cedar" |
| sampleArray.join(" ") | Returns "elm maple ash oak cedar" |
| sampleArray.join(" - ") | Returns "elm - maple - ash - oak - cedar" |
| sampleArray.join(" tree, ") | Returns "elm tree, maple tree, ash tree, oak tree, cedar " |
|
.lastIndexOf() | Syntax: array.lastIndexOf(item,start) | Uses: Returns the index of the last occurance of a value. A start point is optional. |
| sampleArray.lastIndexOf("oak") | Returns 3 |
|
.pop() | Syntax: array.pop() | Uses: Returns the last element from an array and removes it from the array. |
| sampleArray.pop() | Returns "cedar" sampleArray = ["elm", "maple", "ash", "oak"] |
|
.push() | Syntax: array.push(item1, item2, ...) | Uses: Adds new items to the end of an array and returns its length. |
| sampleArray.push("palm", "cherry") | Returns 7 sampleArray = ["elm", "maple", "ash", "oak", "cedar", "palm", "cherry"] |
|
.reverse() | Syntax: array.reverse() | Uses: Reverses the order of elements in an array. |
| sampleArray.reverse() | sampleArray = ["cedar", "oak", "ash", "maple", "elm"] |
|
.shift() | Syntax: array.shift() | Uses: Returns the first item from an array and removes it from the array. |
| sampleArray.shift() | Returns "elm" sampleArray = ["maple", "ash", "oak", "cedar"] |
|
.slice() | Syntax: array.slice(start, end) | Uses: Returns a new array containing items from an existing array. |
| sampleArray.slice(2, 4) | Returns ["ash", "oak"] |
|
.sort() | Syntax: array.sort(function) | Uses: Sorts elements of an array. An optional sort function may be included. |
| sampleArray.sort() | Returns ["ash", "cedar", "elm", "maple", "oak"] |
|
.splice() | Syntax: array.splice(index,num,item1,...) | Uses: Adds or removes items from an array |
| sampleArray.splice(2,2) | Returns ["ash", "oak"] sampleArray = ["elm", "maple", "cedar"] |
| sampleArray.splice(2,2,"walnut","beech") | Returns ["ash", "oak"] sampleArray = ["elm", "maple", "walnut", "beech", "cedar"] |
|
.toString() | Syntax: array.toString() | Uses: Converts an array to a string. |
| sampleArray.toString() | Returns "elm,maple,ash,oak,cedar" |
|
.unshift() | Syntax: array.unshift(item1,item2,...) | Uses: Adds new items to the beginning of an array. Returns the array length. |
| sampleArray.unshift("birch", "spruce") | Returns 7 sampleArray = ["birch", "spruce", "elm", "maple", "ash", "oak", "cedar"] |
sampleArray.sort() | sampleArray = ["ash","cedar","elm","maple","oak"] |
sampleArray.reverse() | sampleArray = ["oak","maple","elm","cedar","ash"] |
sampleArray.push("palm", "cherry") | sampleArray = ["oak","maple","elm","cedar","ash","palm","cherry"] |
sampleArray.unshift("birch", "spruce") | sampleArray = ["birch","spruce","oak","maple","elm","cedar","ash","palm","cherry"] |
sampleArray.pop() | sampleArray = ["birch","spruce","oak","maple","elm","cedar","ash","palm"] |
sampleArray.pop() | sampleArray = ["birch","spruce","oak","maple","elm","cedar","ash"] |
sampleArray.shift() | sampleArray = ["spruce","oak","maple","elm","cedar","ash"] |
sampleArray.shift() | sampleArray = ["oak","maple","elm","cedar","ash"] |
sampleArray.push('locust','Magnolia') | sampleArray = ["oak","maple","elm","cedar","ash","locust","Magnolia","Mulberry"] |
sampleArray.splice(2,2) | sampleArray = ["oak","maple","ash","locust","Magnolia"] |
sampleArray.splice(2,2,"walnut","beech") | sampleArray = ["oak","maple","walnut","beech","Magnolia"] |