To pop the last element in a JavaScript array, you can use the pop()
method. This method removes the last element from the array and returns that element. Here’s an example:
let arr = [1, 2, 3, 4];
let lastElement = arr.pop();
console.log(lastElement); // outputs 4
console.log(arr); // outputs [1, 2, 3]
In this example, pop()
is called on the arr
array, and the last element (4
) is removed and stored in the lastElement
variable. The resulting array is [1, 2, 3]
.
+ There are no comments
Add yours