AppDividend
Latest Code Tutorials

Javascript Array Pop Example | Array.prototype.pop() Tutorial

0

Javascript Array Pop Example | Array.prototype.pop() Tutorial is today’s topic. The javascript pop() method removes the last item from an array and returns that element. This method changes the length of an array. That is why it is not a pure function. Javascript array pop() method changes the length of the array. If we want to remove the first element of an array, use the shift() method.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Javascript Array Pop Example

The pop() method is intentionally generic and can be called or applied to objects resembling arrays. The objects which do not contain the length property reflecting the last in the series of consecutive may not behave in any meaningful manner. If an array is empty, then the removed item is undefined.

Let us take a simple example. Create the app.js file and add the following code.

// app.js

let apps = [
  { name: 'Amazon', value: 1 },
  { name: 'Amazon Prime', value: 21 },
  { name: 'Amazon Music', value: 10 },
  { name: 'Amazon Go', value: 100 },
  { name: 'Amazon Alexa', value: 13 }
];
removedItem = apps.pop();

console.log('The remaining items are: ', apps);
console.log('The removed item is: ', removedItem);

Now, the last item of an array is removed, and we can get that item.

 

Javascript Array Pop Example | Array.prototype.pop() Tutorial

We can see that the item is removed and the length of an array is modified.

We can also perform the same operation with the integers of an array.

// app.js

let numArr = [1, 3, 4, 5, 9];
let removedItem = numArr.pop();

console.log('The remaining items are: ', numArr);
console.log('The removed item is: ', removedItem);

 

Array.prototype.pop() Tutorial

We can do the same for strings of an array.

// app.js

let strArr = ['stan', 'bruce', 'warren', 'michael'];
let removedItem = strArr.pop();

console.log('The remaining items are: ', strArr);
console.log('The removed item is: ', removedItem);

 

Javascript Array Pop() method tutorial

Finally, Javascript Array Pop Example | Array.prototype.pop() Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.