In this article we will learn how to remove an element from an end of array in JavaScript.
It is very common to remove an element from an end of array in JavaScript in JavaScript.
Problem
Now we have a problem, which is that we want to delete an element from the end of the array.
Imagine you have a array like this
const myArray = [1, 2, 3, 4, 5];
And we want to remove an element from the end of an array, to became like this:
[1, 2, 3, 4]
How to solve this problem?
Well, the solution to this problem is very easy, with a built-in function called pop()
.
Remove an element from end of an array using pop
method
As we said before, we can remove an element using pop()
method.
Let's see an example
// Array
const array = [1, 2, 3, 4, 5];
// Remove an element
array.pop();
// Result:
console.log(array);
Output
[ 1, 2, 3, 4 ]
The pop() method removes the last element from an array and returns that element. This method changes the length of the array. MDN
Thank you for reading
Thank you for reading my blog. 🚀