JavaScript Array Type

Deletion

We can use the pop method to delete elements at the end or shift to delete elements at the start of the array. Both of these methods return the element they removed and end up mutating the array.

const horse = ["He", "ate", "my", "horse."]
console.log(horse.pop()) // "horse."
console.log(horse) // ["He", "ate", "my"]
console.log(horse.shift()) // "He"
console.log(horse) // ["ate", "my"]

Functional deletion

There are ways we can emulate deletion without mutating the original array. The most straight forward way is probably using slice.

The slice method returns a copy of a portion of an array given a start and an end index.

Code example: slice

const horse = ["He", "ate", "my", "horse."]
console.log(horse.slice(3)) // "horse."
console.log(horse) // ["He", "ate", "my", "horse."]
console.log(horse.slice(0, 1)) // "He"
console.log(horse) // ["He", "ate", "my", "horse."]