1var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
2var citrus = fruits.slice(1, 3);
3// ["Orange", "Lemon"]
1// array.slice(start, end)
2const FRUITS = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
3var citrus = FRUITS.slice(1, 3);
4// citrus => [ 'Orange', 'Lemon' ]
5
6// Negative values slice in the opposite direction
7var fromTheEnd = FRUITS.slice(-3, -1);
8// fromTheEnd => [ 'Lemon', 'Apple' ]
1var str = "12,15,16,78,59";
2var res = str.split(",");
3console.log(res[0]); // result: 12
4console.log(res[2]); // result: 16
1//The slice() method extracts a section of a string and returns
2//it as a new string, without modifying the original string.
3
4// same in array but you select elements not characters
5
6
7const str = 'The quick brown fox jumps over the lazy dog.';
8
9console.log(str.slice(31));
10// expected output: "the lazy dog."
11
12console.log(str.slice(4, 19));
13// expected output: "quick brown fox"
14
15console.log(str.slice(-4));
16// expected output: "dog."
17
18console.log(str.slice(-9, -5));
19// expected output: "lazy"
20
21console.log(str.slice(0, 2));
22// expected output: "the"
23// Up to and including the last index!!!
24// Different for python.
1var oldColors=["red","green","blue"];
2var newColors = oldColors.slice(); //make a clone/copy of oldColors
1const string="Hi my name is mezen";
2string.slice (2); // return string without the character of index 2;
3string.slice (6); // return string without the character of index 6;
4
5string.slice (3,7) /* return 'my na' (m of index 3; a of index 7) including the
6 blank spaces */