When dealing with arrays, it’s simple to both remove elements and add new ones.
This is achieved through “popping,” which involves removing items from an array, and “pushing,” which involves adding items into an array.
The pop() method eliminates the last element from an array.
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.pop(); |
The pop() method returns the value that was removed from the array.
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; let fruit = fruits.pop(); |
The push() method appends a new element to the end of an array.
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.push(“Kiwi”); |
The push() method returns the updated length of the array.
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; let length = fruits.push(“Kiwi”); |