8 3 1 common array methods 2f 2f slice examples 28 slice 29

Solutions on MaxInterview for 8 3 1 common array methods 2f 2f slice examples 28 slice 29 by the best coders in the world

showing results for - "8 3 1 common array methods 2f 2f slice examples 28 slice 29"
Ilaria
03 Oct 2020
1//The general syntax for this method is:
2arrayName.slice(starting index, ending index)
3
4/*This method copies a range of elements from one array into a new array. 
5slice does NOT change the original array, but returns a new array.
6
7The ending index is optional. If it is left out, slice returns a new 
8array that includes everything from the starting index to the end of the
9original array.
10
11If both indices are used, the new array contains everything from the
12starting index up to, but NOT including the ending index.*/
13
14let arr = ['a', 'b', 'c', 'd', 'e'];
15
16arr.slice(2);
17
18arr.slice(1,4);
19
20console.log(arr);
21
22//[ 'c', 'd', 'e' ]
23//[ 'b', 'c', 'd' ]
24//['a', 'b', 'c', 'd', 'e']
similar questions