Languages
[Edit]
EN

JavaScript - remove last item from array

0 points
Created by:
May87
827

In this article, we would like to show you how to remove the last item from array using JavaScript​​​​​​.

1. Using splice() method

Edit

In this example, to remove the last element from the array we use splice() method. 

Note:

array.splice(-1, 1); is equal to array.splice(array.length - 1, 1);.

2. Using pop() method

Edit

In this example, we use pop() method that removes the last element from array and returns that element.

3. Using slice() method

Edit

In this example, we use slice() method to create a shallow copy of an array from indexStart to indexEnd (not included).

Warning:

This approach doesn't really remove the last element, it copies the array omitting it.

References

Edit
  1. Array.prototype.splice() - JavaScript | MDN
  2. Array.prototype.slice() - JavaScript | MDN
  3. Array.prototype.pop() - JavaScript | MDN

Alternative titles

  1. JavaScript - remove last element from array
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join