Free Training


C Language  |  CSS  |  MainFrame  |  VBScript  |  PHP  |  XML  |  C++ Tutorials  |  Ajax  |  JavaScript  |  CSS3  |  UML  |  jQuery  |  Microsoft AJAX

JavaScript Tutorials

 
Home Tutorials JavaScript
 

JavaScript Array Object Methods – Part II

 

JavaScript Array Object Methods – Part II

Page 1 of 2

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



<html>
   <
body>
      <
script type="text/javascript">


         var exforsys = new Array(4)
         exforsys[0] = "Welcome"
         exforsys[1] = "To"
         exforsys[2] = "Exforsys"
         exforsys[3] = "Training"
         document.write(exforsys + "<br />")
         document.write(exforsys.slice(1) + "<br />")
         document.write(exforsys)

      </script>
   </
body>
</
html>



 


The output of the code above will be:



Welcome,To,Exforsys,Training
To,Exforsys,Training
Welcome,To,Exforsys,Training



 


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



<html>
   <
body>
      <
script type="text/javascript">
         var exforsys = new Array(4)
         exforsys[0] = "Welcome"
         exforsys[1] = "To"
         exforsys[2] = "Exforsys"
         exforsys[3] = "Training"
         document.write(exforsys + "<br />")
         exforsys.splice(1,0,"All")
         document.write(exforsys+ "<br />")
     
      </
script>
   </
body>
</
html>



 


The output of the code above will be:



Welcome,To,Exforsys,Training
Welcome,All,To,Exforsys,Training



 


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



<html>
   <
body>
      <
script type="text/javascript">
         var exforsys = new Array(4)
         exforsys[0] = "Welcome"
         exforsys[1] = "To"
         exforsys[2] = "Exforsys"
         exforsys[3] = "Training"
         document.write(exforsys.toString())

      </
script>
   </
body>
</
html>



 



The output of the code above will be:



Welcome,To,Exforsys,Training



 


In the above example, the array object exforsys is converted to a string using the toString() method of the array object.


Next Page: JavaScript Array Object shift() and unshift()


Read Next: Working with JavaScript DOM Objects



 
Related Topics

Related Jobs


 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Weekly Offers

Sponsored Links