Tutorials Point

  JavaScript Basics
  JavaScript Objects
  JavaScript Advanced
  JS Useful References
  JS Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint

  Home     References     Discussion Forums     About TP  

Javascript Array slice() Method

previous next AddThis Social Bookmark Button



Javascript array slice() method extracts a section of an array and returns a new array.


array.slice( begin [,end] );

Here is the detail of parameters:

  • begin : Zero-based index at which to begin extraction. As a negative index, start indicates an offset from the end of the sequence.

  • end : Zero-based index at which to end extraction.

Return Value:

Returns the extracted array based on the passed parameters.


<title>JavaScript Array slice Method</title>
<script type="text/javascript">
var arr = ["orange", "mango", "banana", "sugar", "tea"];
document.write("arr.slice( 1, 2) : " + arr.slice( 1, 2) ); 
document.write("<br />arr.slice( 1, 3) : " + arr.slice( 1, 3) ); 

This will produce following result:

arr.slice( 1, 2) : mango
arr.slice( 1, 3) : mango,banana 

To understand it in better way you can Try it yourself.

previous next Printer Friendly