Push method vs. Pop method in Javascipt!

Faizah Ahsan
2 min readDec 13, 2020

--

If you are new in the software developer world, these two (Push()) & Pop()) methods Are very important. You will probably be using these methods inside a lot of function to solve a lot of problems. So let’s find out what each of these actually does and how they differ from each other.

.push()

The push method adds items to the end of an array. This method appends one or more value to the last position of an array. This method mutates the array, returning the new length of the array.

Example:

// List of students in an array let students = [‘mike’, ‘will’, ‘shawn’, ‘bill’] // Add a new name to list let newList = students.push(‘liz’) // Return the length of the array console.log(newList)

The push method:

  • Takes a value or values as arguments.
  • Adds the value(s) to the end of the array.
  • Returns the new length of the array.
  • If the array is empty, it returns 0

Here is another example:

.pop()

The pop() method removes the last element of an array and returns that element. It will remove an item from the end of an array and return that item. The pop method can be called or applied to objects that resembling arrays. Objects which do not contain the length property reflecting a last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner. If you call a pop() method on the empty array, it returns as undefined. This function decreases the length of the array by 1.

In the example above, you can see when we applied the .pop() method on the array it took out the last element which was number 6 and mutated the original array.

--

--

Faizah Ahsan
Faizah Ahsan

Written by Faizah Ahsan

Junior Full-Stack Web Developer. Looking for opportunities!

No responses yet