Copy part of an array with slice()

suggest change

To copy part of an array:

It returns a new array filled with items from arr.

begin is 0-based index of the first item.

end is 0-based index of the last item

If only begin is given, slice copies everything until the end of array

If end is negative it becomes end = arr.length + end i.e. -1 would leave off last element of array, -2 last 2 elements etc.

if end is ≤ begin, returns an empty array.

const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

let newArr = arr.slice(0, 2);
console.log("slice(0,2)  :", newArr)

newArr = arr.slice(8);
console.log("slice(8)    :", newArr)

newArr = arr.slice(5, -1);
console.log("slice(5, -1):", newArr)

newArr = arr.slice(6, -2);
console.log("slice(6, -2):", newArr)

newArr = arr.slice(3, 3)
console.log("slice(3, 3) :", newArr)

newArr = arr.slice(4, 1)
console.log("slice(4, 1) :", newArr)
slice(0,2)  : [ 0, 1 ]
slice(8)    : [ 8, 9 ]
slice(5, -1): [ 5, 6, 7, 8 ]
slice(6, -2): [ 6, 7 ]
slice(3, 3) : []
slice(4, 1) : []

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Arrays:
* Syntax
* Copy part of an array with slice()

Table Of Contents
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde