Deleting Items from an Array using JavaScript

Leave a Comment

If you want to remove an item from the end or beginning of an array, use the pop() or shift() commands. Both commands remove one item from the array: The pop() command removes the item from the end of the array, while shift() removes one item from the beginning.

for example, this code removes a value and stores it in the variable removedItem:
var p = [0,1,2,3];
var removedItem = p.pop();

The value of removedItem after this code runs is 3 and the array p now contains
[0,1,2].

0 comments:

Post a Comment