push() method and the
unshift() method. The function and syntax of the methods are shown below.
- Inserts a new element at the tail of the array
- Inserts a new element at the head of the array
unshift() both add an element to the original array. As mentioned above, the
push() method adds the element to the end of the array. This is why version 1 of the array is [2,3,4,5]. The
unshift() method on the other hand adds the element to the start of the array. Therefore, version 2 of the array ends up being [1,2,3,4,5]. These two method are two separate methods and should be used only when it is appropriate. You will probably end up using the
push() method more often, but don't use these two methods interchangeably.