HTML and CSS Reference
In-Depth Information
Figure 9.12 An associative array—one key associated with more than one value.
9.3 Array Methods
Because an array is an object in JavaScript, it has properties to describe it and methods
to manipulate it. The length property of an array was used in previous examples to deter-
mine the size of an array. Now we will look at methods that allow you to manipulate
arrays such as adding a new element at the beginning or end of an array, removing an
element from the end of an array, reversing an array, and so on. JavaScript provides a
whole set of methods for doing all of these things and more (see Table 9.2).
Table 9.2 Array Methods
Method
What It Does
concat()
Concatenates elements from one array to another array.
join()
Joins the elements of an array by a separator to form a string.
pop()
Removes and returns the last element of an array.
push()
Adds elements to the end of an array.
reverse()
Reverses the order of the elements in an array.
shift()
Removes and returns the first element of an array.
slice()
Creates a new array from elements of an existing array.
sort()
Sorts an array alphabetically or numerically.
splice()
Removes and/or replaces elements of an array.
toLocaleString()
Returns a string representation of the array in local format.
toString()
Returns a string representation of the array.
unshift()
Adds elements to the beginning of an array.
 
 
Search WWH ::




Custom Search