JavaScript slice() Method
JavaScript Array Object
Definition and Usage
The slice() method returns selected elements from an existing array.
Syntax
|
arrayObject.slice(start,end)
|
| Parameter |
Description |
| start |
Required. Specify where to start the selection. Must
be a number. Starts at 0 |
| end |
Optional. Specify where to end the selection. Must
be a number |
Tips and Notes
Tip: You can use negative numbers to select from the end of the array.
Note: If end is not specified, slice() selects all elements from the
specified start position and to the end of the array.
Example
Example
In this example we will create an array, and then display selected elements from it:
<script type="text/javascript">
var arr = new Array(6);
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";
arr[5] = "Tove";
document.write(arr + "<br />");
document.write(arr.slice(0,1) + "<br />");
document.write(arr.slice(2,4) + "<br />");
document.write(arr);
</script>
|
The output of the code above will be:
Jani,Hege,Stale,Kai Jim,Borge,Tove
Jani
Stale,Kai Jim
Jani,Hege,Stale,Kai Jim,Borge,Tove
|
Try it yourself »
|
JavaScript Array Object

Whether you're new to XML or already an advanced user,
the user-friendly views and powerful entry helpers,
wizards, and debuggers in XMLSpy are designed to meet your XML
and Web development needs from start to finish.
New features in Version 2010!
- XML editor
- Graphical XML Schema / DTD editors
- XSLT 1.0/2.0 editor, debugger, profiler
- XQuery editor, debugger, profiler
- XBRL validator, taxonomy editor, taxonomy wizard
- Support for Office Open XML (OOXML)
- Graphical WSDL 1.1/2.0 editor & SOAP debugger
- JSON editing & conversion
- Java, C#, C++ code generation
- And much more!
Download a free trial today!
|
|
|
|