how to use slice method in javascript code example

Example 1: javascript get sub array

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1, 3);
// ["Orange", "Lemon"]

Example 2: javascript slice

//The slice() method extracts a section of a string and returns 
//it as a new string, without modifying the original string.

// same in array but you select elements not characters  


const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31));
// expected output: "the lazy dog."

console.log(str.slice(4, 19));
// expected output: "quick brown fox"

console.log(str.slice(-4));
// expected output: "dog."

console.log(str.slice(-9, -5));
// expected output: "lazy"

console.log(str.slice(0, 2)); 
// expected output: "the"
// Up to and including the last index!!!
// Different for python.

Example 3: js slice

const arr=[1,2,3,4,5];

const slicedArr = arr.slice(1,4); // slicedArr = [2,3,4]

Example 4: array index javascript show only first 2 elements

array.slice(0, n);

Example 5: slice in javascript

JavaScript Array slice() Method The slice() method returns the selected elements in an array, as a new array object. The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument. Note: The original array will not be changed.