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' ]
1 [] // Array object
2.slice // Accessing the function 'slice' present in the prototype of Array
3.call // Accessing the function 'call' present in the prototype of function object(slice)
4(document.querySelectorAll('a'),0)
5 // 'call' can have arguments like, (thisArg, arg1,arg2...n).
6 // So here we are passing the 'thisArg' as an array like object,
7 // that is a 'nodeList'. It will be served as 'this' object inside of slice function.
8 // And finally setting 'start' argument of slice as '0' and leaving the 'end'
9// argument as 'undefined'
10
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.
1const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
2
3console.log(animals.slice(2));
4// expected output: Array ["camel", "duck", "elephant"]
5
6console.log(animals.slice(2, 4));
7// expected output: Array ["camel", "duck"]
8
9console.log(animals.slice(1, 5));
10// expected output: Array ["bison", "camel", "duck", "elephant"]
1var str1 = "Apple, Banana, Kiwi";
2
3var res1 = str1.slice(7, 13);
4var res2 = str1.slice(-12, -6);
5var res3 = str1.slice(7);
6var res4 = str1.slice(-12);
7var res5 = str1.substring(7, 13); //substring() cannot accept negative indexes like slice.
8var res6 = str1.substr(7, 9); // substr(start, length) is similar to slice(). second parameter of substr is length of string
9
10document.write("<br>" + "slice:", res1);
11document.write("<br>" + "slice -ve pos:", res2);
12document.write("<br>" + "slice with only start pos:", res3);
13document.write("<br>" + "slice with only -ve start pos", res4);
14document.write("<br>" + "substring:", res5);
15document.write("<br>" + "substr:", res6);
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