The slice() Method code example

Example 1: 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 2: js copy part of array

// The slice function copies parts of an array and 
// do therefore not change the default array

let arr = ["apple", "kiwi", "banana", "pear"];

let newArray = arr.slice(1, 2)

console.log(newArray); // output : kiwi