Skip to content

Latest commit

 

History

History
75 lines (61 loc) · 2.19 KB

File metadata and controls

75 lines (61 loc) · 2.19 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
slice Method (Array) (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
slice
JavaScript
TypeScript
DHTML
zero-based index
Array object
slice method
3c122219-14de-4126-b091-809659c026d6
14
mikejo5000
mikejo
ghogen

slice Method (Array) (JavaScript)

Returns a section of an array.

Syntax

  
arrayObj.slice(start, [end])   

Parameters

arrayObj
Required. An Array object.

start
Required. The beginning of the specified portion of arrayObj.

end
Optional. The end of the specified portion of arrayObj.

Remarks

The slice method returns an Array object containing the specified portion of arrayObj.

The slice method copies up to, but not including, the element indicated by end. If start is negative, it is treated as length + start, where length is the length of the array. If end is negative, it is treated as length + end where length is the length of the array. If end is omitted, extraction continues to the end of arrayObj. If end occurs before start, no elements are copied to the new array.

Example

The following examples show how to use the slice method. In the first example, all but the last element of myArray is copied into newArray. In the second example, only the last two elements of myArray are copied into newArray.

var origArray = [3, 5, 7, 9];  
var newArray = origArray. slice(0, -1);  
document.write(origArray);  
document.write("<br/>");  
newArray = origArray. slice(-2);  
document.write(newArray);  
  
// Output:  
// 3,5,7,9  
// 7,9  
  

Requirements

[!INCLUDEjsv3]

See Also

slice Method (String)
String Object