Manipulating Arrays with the Push() and Pop() Methods

Guest Posts Paolo Nikko Nuñal

Arrays are great for handling many numbers. Most especially if you are dealing with numerous numbers in handling out process. In this example, we are going to learn some pre-define methods in JavaScript that deals with array. This time around we are going to learn how the push() and pop() method works.

Basically the push() method inserts data at the end of the array and the pop() method removes the last element of the array.

Code


<script>
   var dataaRRAY = new Array("John","Joe","Joseph");

   dataArray.push("Jonard");
   alert(dataArray);
</script>


The result of the code will go like this, the word Jonard will be placed on the last part of the array or at index [3]. The push() method is a great way to insert data if you don't care on the position. This is also been used in most data structure problems. Additionally this method also changes the length of the array as there has been additional contents being inserted.

On the other hand, the pop() method removes the last element of the array.

Code


<script>
   var dataaRRAY = new Array("John","Joe","Joseph");

   dataArray.push();
   alert(dataArray);
</script>


The pop method will remove the last item on the array and in our case "Joseph" will be removed.

The pop() and push() methods are commonly used for FIFO (First in First Out) or LIFO (Last in First Out) problems. And can also be used if you are handling numerous items.