let fruits = ['orange','banana','mango','cherry','strawberry','apple']; let fvrtFruits = fruits.slice(1,4); console.log(fvrtFruits); // ['banana','mango','cherry','strawberry'];
slice() is used on an array of array elements to extract out of the arrays. The contents are modified and both arrays are printed out. The changes to the new array are reflected in the old:
let fruitsArray = ; fruitsArray = ['mango','pear']; fruitsArray = ['strawberry','lemon']; fruitsArray = ['lime','peach','berry']; var nArray = mArray.slice(1,2); console.log(mArray); // ['strawberry','lemon'] nArray = 'banana'; console.log(nArray); // ['banana','lemon']
The values are copied by reference. If the array element is a primitive data type, such as a string or number, the elements are copied by value—changes to the new array won’t be reflected in the old.