es6 slice code example
Example 1: javascript copy array
var oldColors=["red","green","blue"];
var newColors = oldColors.slice(); //make a clone/copy of oldColors
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: slice()
/"slice() copies or extracts a given number of elements to a new array"/
let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
let todaysWeather = weatherConditions.slice(1, 3);
// todaysWeather equals ['snow', 'sleet'];
// weatherConditions still equals ['rain', 'snow', 'sleet', 'hail', 'clear']
Example 4: slice() javascript
let ourString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
ourString.slice(0, 11);
console.log(ourString.slice(0, 11));
//Lorem ipsum
string.slice(start, end);
//Start is required. The position where to begin the extraction.
//First character is at position 0
//End is optional.
//The position (up to, but not including) where to end the extraction.