Answers for "8.3.1. Common Array Methods // slice Examples (.slice)"

0

8.3.1. Common Array Methods // slice Examples (.slice)

//The general syntax for this method is:
arrayName.slice(starting index, ending index)

/*This method copies a range of elements from one array into a new array. 
slice does NOT change the original array, but returns a new array.

The ending index is optional. If it is left out, slice returns a new 
array that includes everything from the starting index to the end of the
original array.

If both indices are used, the new array contains everything from the
starting index up to, but NOT including the ending index.*/

let arr = ['a', 'b', 'c', 'd', 'e'];

arr.slice(2);

arr.slice(1,4);

console.log(arr);

//[ 'c', 'd', 'e' ]
//[ 'b', 'c', 'd' ]
//['a', 'b', 'c', 'd', 'e']
Posted by: Guest on June-16-2021

Code answers related to "8.3.1. Common Array Methods // slice Examples (.slice)"

Code answers related to "Javascript"

Browse Popular Code Answers by Language