JavaScript: Array Adding Method Unshift() vs Push()

Using unshift function to add elements to Array is quite similar with push function. They both return the new length property of the array upon which the method was called except unshift function adds elements to the beginning of the array instead of the end.

The unshift method will add an element to the BEGINNING of an array, while its twin function, shift(), will remove one element from the BEGINNING of the array.

The push() method will add an element to the END of an array, while its twin function, the pop() method, will remove an element from the END of the array.

Summary

  • unshift/push – Add an element to the beginning/end of an array
  • shift/pop – Remove and return the first/last element of and array

A simple diagram as below:

and chart:

Check out the MDN Array documentation.