slice use in javascript code example
Example 1: 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 2: slice javascript
const string="Hi my name is mezen";
string.slice (2); // return string without the character of index 2;
string.slice (6); // return string without the character of index 6;
string.slice (3,7) /* return 'my na' (m of index 3; a of index 7) including the
blank spaces */
Example 3: 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.