Question about arrays

Hello everyone!

I have a question about the array methods.
When we use unshift() do we have to use shift() first?

1 Like

No we don’t. shift() deletes from first index in an array, unshift() adds to the first position. Say i have this array called myArray = [0,1,2,3], and i decide to kick 0 out of my array, i would just type myArray.shift(). This leaves me with myArray = [1,2,3].
six seconds later or almost immediately, i could decide to throw zero back(since it is a number either-ways) or any other number, i would now say myArray.unshift(0); which brings my array back to myArray = [0,1,2,3].

Now, you see, i could use both methods if the need be, but i dont have to :grin: