JavaScript provides a number of methods that allow you to manipulate arrays.
The join() Method
This converts each element in an array into a string then concatenates them.
The method may be supplied with an optional string to separate the elements. If this isn't supplied then the default is a comma.
Example of join()
<script language="javascript" type="text/javascript">
var names = new Array ("Tom", "Dick", "Harry");
list = names.join(" and ");
// This prints out Tom and Dick and Harry
document.write (list);
</script>
The reverse() Method
This works directly on an array and reverses the order of the elements within the array.
Example of reverse()
<script language="javascript" type="text/javascript">
var names = new Array ("Tom", "Dick", "Harry");
names.reverse();
// This prints out Harry
document.write(names[0]);
</script>
The concat() Method
This method is used to join two or more arrays into one array.
Example of concat()
<script language="javascript" type="text/javascript">
var names = new Array ("Tom", "Dick", "Harry");
var persons = new Array ("Mary", "Janet", "Elsie");
list = names.concat(persons);
// This prints out Janet
document.write (list[4]);
</script>
The slice() Method
This is used to return a slice of an array and normally takes two arguments.
The start and end indexes of the slice. The returned slice includes the element specified by the first argument and all elements up to, but not including, the element specified by the second argument.
If only one argument is supplied then the slice includes the element specified by the first argument and all elements up to the end of the array.
If either argument is negative then it specifies an element relative to the end of the array.
Example of slice()
<script language="javascript" type="text/javascript">
var names = new Array ("Tom", "Dick", "Harry", "Fred", "James");
list = names.slice(0,3);
// This prints out Tom,Dick,Harry
document.write (list);
</script>
The splice() Method
This works directly on an array and is used to remove or insert elements. To remove elements only two arguments are required.
The first argument specifies where the insertion or removal is to begin. The second element specifies the number of elements to remove.
These two arguments may optionally be follow by any number of elements to be inserted at the start position specified by the first argument.
Example of a splice() Removal
<script language="javascript" type="text/javascript">
var names = new Array ("Tom", "Dick", "Harry", "Fred", "James");
names.splice(1,1);
// This prints out Tom,Harry,Fred,James
document.write (names);
</script>
Example of a splice() Insertion
<script language="javascript" type="text/javascript">
var names = new Array ("Tom", "Dick", "Harry", "Fred", "James");
names.splice(1,0,"Mary","Jane");
// This prints out Tom,Mary,Jane,Dick,Harry,Fred,James
document.write (names);
</script>
The push() and pop() Methods
These methods work directly on an array to simulate a stack. Push, pop and stack are more familiar to low level programmers.
The concept of a stack may be visualised as one of those spikes on which receipts are skewered.
When you add a receipt it gets pushed onto the top of the stack. To retrieve a receipt from the centre you must first pop off those on top.
This is an example of a Last-In-First-Out (LIFO) stack, which can be implemented on an array with push() and pop().
Example of push()
<script language="javascript" type="text/javascript">
var stack = new Array ("Tom", "Dick", "Harry", "Fred", "James");
stack.push("Mary","Jane");
// This prints out Tom,Dick,Harry,Fred,James,Mary,Jane
document.write (stack);
</script>
Example of pop()
<script language="javascript" type="text/javascript">
var stack = new Array ("Tom", "Dick", "Harry", "Fred", "James");
stack.pop();
// This prints out Tom,Dick,Harry,Fred
document.write (stack);
</script>
The shift() and unshift() Methods
These are similar to the push() and pop() methods except that...
Elements are removed or added to the beginning of the array instead of the end, simulating a First-In-First-Out (FIFO) stack.
The unshift() method allows you to add more than one element to the beginning of the array and adds them in the order that they appear in the argument.
Example of shift()
<script language="javascript" type="text/javascript">
var stack = new Array ("Tom", "Dick", "Harry");
stack.shift();
// This prints out Dick,Harry
document.write (stack);
</script>
Example of unshift()
<script language="javascript" type="text/javascript">
var stack = new Array ("Tom", "Dick", "Harry");
stack.unshift("Fred");
// This prints out Fred,Tom,Dick,Harry
document.write (stack);
</script>
The toString() Method
This method converts each element in the array to a string and concatenates them into a list separated by commas.
Example of toString()
<script language="javascript" type="text/javascript">
var numbers = new Array (1,2,3,4,5);
result = numbers.toString();
// This prints out 1,2,3,4,5
document.write (result);
</script>