To remove the returned item without re-addressing the array and invalidating all references to it, shift() requires moving the entire array around; pop() can simply subtract 1 from its length.
To remove the returned item without re-addressing the array and invalidating all references to it, shift() requires moving the entire array around; pop() can simply subtract 1 from its length.
shift() has to re-index the whole array while pop() doesn't.
pop() simply removes the last element in the array. Therefore, the elements do not move; simply the .length has to be updated.
shift() removes the first element in the array. This requires a re-indexing of all elements in the array, so that [1] becomes [0] and so on.
Videos
Javascript has a number of methods related to arrays which allow programmers to perform various array operations. There are four methods which are particularly used for adding and removing elements to and from an array. They are: push(), pop(), shift() and unshift(). For an experienced as well as new programmers, its likely to sometimes get confused how each of them work and which one to use in which situation. Thus, in this article, we have tried to simplify the concept with pictures and examples. Let's start exploring them one by one. Then we will compare their similarities and differences. Please look at the pictures too for better understanding.
https://foxbits.dev/article/four-common-javascript-array-methods-push-pop-shift-and-unshift/15