Availability
JavaScript 1.2; JScript 3.0; ECMAScript v3
Synopsis
array.slice(start, end)Arguments
start
The array index at which the slice is to begin. If negative, this
argument specifies a position measured from the end of the array.
That is, -1 indicates the last element, -2 indicates the second from
last element, and so on.
end
The array index immediately after the end of the slice. If not
specified, the slice includes all array elements from the
start to the end of the array. If this
argument is negative, it specifies an array element measured from the
end of the array.
Returns
A new array that contains the elements of
array from the element specified by
start, up to, but not including, the
element specified by end.
Description
slice( ) returns a slice, or subarray, of
array. The returned array contains the
element specified by start and all
subsequent elements up to, but not including, the element specified
by end. If end
is not specified, the returned array contains all elements from the
start to the end of
array.
Note that slice( ) does not modify the array. If
you want to actually remove a slice of an array, use
Array.splice( ).
Example
var a = [1,2,3,4,5];
a.slice(0,3); // Returns [1,2,3]
a.slice(3); // Returns [4,5]
a.slice(1,-1); // Returns [2,3,4]
a.slice(-3,-2); // Returns [3]; buggy in IE 4: returns [1,2,3]
Bugs
start cannot be a negative number in
Internet Explorer 4.
See Also
Array.splice( )
•
Table of Contents
•
Index
•
Reviews
•
Examples
•
Reader Reviews
•
Errata
JavaScript: The Definitive Guide, 4th Edition
By
David Flanagan
Publisher
: O'Reilly
Pub Date
: November 2001
ISBN
: 0-596-00048-0
Pages
: 936
Slots
: 1
This fourth edition of the definitive reference to
JavaScript, a scripting language that can be embedded
directly in web pages, covers the latest version of the
language, JavaScript 1.5, as supported by Netscape 6 and
Internet Explorer 6. The book also provides complete
coverage of the W3C DOM standard (Level 1 and Level 2),
while retaining material on the legacy Level 0 DOM for
backward compatibility.