JavaScript Array Object Methods – Part II
In this JavaScript tutorial, you will learn about on array object methods – slice(), splice() toString(), shift() and unshift() methods along with general syntax and examples.
slice():
By using the methods of the array object the programmer is able to retrieve the first and the last elements in an array object. Suppose a programmer wishes to retrieve selected elements from an existing array. This can be performed by using the slice() method of the array object. The slice method creates a new array with the selected elements.
General syntax of the slice() method of the Array object of JavaScript:
arrayObject.slice(begining,end)
The beginning and the end parameters specified in the slice() method above denote where to start the selection of elements for retrieving and where to end the selection of elements for retrieving. The beginning parameter is mandatory and must be specified. The end parameter is optional. If the end parameter is not specified in the slice() method, then everything from the position mentioned in the beginning parameter until the end of the array is retrieved. The beginning and the end parameters are represented as numbers representing the index or the position of the array elements.
Example
|
The output of the code above will be:
|
In the above example, the array object exforsys elements are first outputted and then slice(1) method is used with the array object exforsys. This retrieves the elements from the first position of the array object exforsys. Since no end parameter is specified, all elements of the array from the first position until the end position are retrieved and then outputted as "To,Exforsys,Training". Printing of the array object exforsys prints the original array as slice() method does not change the original array.
splice():
If the programmer wishes to add and/or remove elements of an array, then the splice() method can be used along with the array object.
General syntax of the splice() method of the Array object of JavaScript:
arrayObject.splice(index,howmany,element1,…..,elementn)
In this example, index and howmany are the mandatory parameters. The element1,…..,elementn are optional. The index argument is a number which represents the position where to add or from where to remove elements in an array object. The howmany parameter is also a number and represents the number of elements to be removed. Optionally, element1,…..,elementn represent the new elements to be added.
Example
|
The output of the code above will be:
|
In the above example, the array object exforsys elements are first outputted and splice(1,0,"All") specifies that the new element "All" is to be added to the first position of the array object exforsys. Shown as the result in the above example, the new array with the added element is printed.
toString():
The method toString() is used to convert an array to a string. The elements in the array are separated by commas and the result is then returned.
General syntax of the toString() method of the Array object of JavaScript:
arrayObject.toString()
Example
|
The output of the code above will be:
|
In the above example, the array object exforsys is converted to a string using the toString() method of the array object.
shift():
As the pop() method removes and returns the last element, the shift() method removes and returns the first element of an array object. Since the first element is removed and returned this also affects the length of the array.
General syntax of the shift() method of the Array object of JavaScript:
arrayObject.shift()
Example
|
The output of the code above will be:
|
In the above example, the array object exforsys elements are first outputted and the shift() method is then used with the array object exforsys which removes and returns the first element of the array object exforsys (Training). The next document statement prints the array without the first element since it has been removed out in the previous statement.
unshift():
If a programmer wants to add one or more elements to the end of the array, he can use the push() method of the array object. There may be situations where a programmer might wish to add one or more elements to the beginning of the array. This can be achieved by using the unshift() method of the array object. The unshift() method of an array object is used to add one or more elements to the beginning of an array and returns the new length.
General syntax of the unshift() method of the Array object of JavaScript:
arrayObject.unshift(newelement1,newelement2,….,newelementn)
The newelement1, newelement2,…., newelementn represents the new elements to be added to the array object. It is required that at least newelement1 must be present. This means there must be at least one element specified in argument of the unshift() method to be added. The newelement2,….,newelementn are optional.
Example
|
The output of the code above will be:
|
In the above example, the array object exforsys elements are first outputted and then the unshift method() used with the exforsys array object has in its argument "Welcome Message". This is added to the beginning of the array object exforsys and changes the length of the array exforsys to 5. This prints and the new array with "Welcome Message" added to the beginning of the array is outputted.