HTML and CSS Reference
Using Array Methods
• To reverse the order of items in an array, use the method
where array is the name of the array.
• To sort an array in alphabetical order, use the following method:
• To sort an array in any order, use
array .sort( function )
where function is the name of a compare function that returns a positive, negative,
or 0 value.
• To extract items from an array without affecting the array contents, use
array .slice( start , stop )
where start is the index of the array item at which the slicing starts and stop is the
index before which the slicing ends. If no stop value is provided, the array is sliced to
the end of the array.
• To remove items from an array, use
array .splice( start , size )
where start is the index of the array item at which the splicing starts and size is the
number of items to remove from the array. If no size value is specified, the array is
spliced to its end.
• To add items to an array, use
array . splice ( start , size , values)
where values is a comma-separated list of new values to replace the old values in
• To add new items to the end of an array, use
array .push( values )
where values is a comma-separated list of values.
• To remove the last item from an array, use the following method:
Figure 12-11 summarizes several other methods that can be applied to arrays. Arrays
with arrays can be used to simplify and expand the capabilities of Web page scripts.