Technical Training
JavaScript TutorialTable of Contents
JavaScript Array Object Methods – Part II
JavaScript Array Object shift() and unshift()JavaScript Array Object Methods – Part II
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.
JavaScript Tutorial
- JavaScript Browser Objects Part 2
- JavaScript Frame object
- JavaScript Form Object
- JavaScript FileUpload Object
- JavaScript Event Object Properties and Methods
- JavaScript Event Object
- JavaScript Elements and Embed Objects
- JavaScript Applet Objects
- JavaScript Browser Objects
- JavaScript Object Oriented Features
- JavaScript Window Object Open Method Part 2
- JavaScript Window Object Open Method
- JavaScript Window Object Timeout Methods
- JavaScript Location Object
- JavaScript Location Object Properties
- JavaScript History Object Properties and Methods
- JavaScript Document Object Methods Part II
- JavaScript Document Object Methods Part I
- JavaScript Document Object Properties
- JavaScript Document Object
- JavaScript Windows Object Properties Part II
- JavaScript Windows Object Properties Part I
- JavaScript DOM Window Object
- Working with JavaScript DOM Objects
- JavaScript Array Object Methods – Part II
- JavaScript Array Object
- JavaScript Array Object Methods – Part I
- JavaScript Boolean Object
- JavaScript OnError Event
- JavaScript Exception Handling – Part II
- JavaScript Exception Handling – Part I
- JavaScript Event Handler
- JavaScript Events Handling
- JavaScript Array Operations
- JavaScript Two Dimensional Arrays
- Passing values to JavaScript Function
- JavaScript Functions
- JavaScript Arrays
- JavaScript Iterative Structures - Part II
- JavaScript Iterative Structures - Part I
- JavaScript Math Object
- JavaScript Date Object
- JavaScript String Object
- JavaScript Objects
- JavaScript Confirm Box
- JavaScript Alert Box
- JavaScript Conditional Statements Part 2
- JavaScript Conditional Statements Part 1
- How to use JavaScript in HTML page
- JavaScript Variables
- JavaScript Features
- JavaScript Introduction







