.push() allows us to add items to the end of an array. Here is an example of how this is used:
const itemTracker = ['item 0', 'item 1', 'item 2']; itemTracker.push('item 3', 'item 4'); console.log(itemTracker); // Output: ['item 0', 'item 1', 'item 2', 'item 3', 'item 4'];
So, how does
- We access the
pushmethod by using dot notation, connecting
itemTrackerwith a period.
- Then we call it like a function. That’s because
.push()can take a single argument or multiple arguments separated by commas. In this case, we’re adding two elements:
- Notice that
.push()changes, or mutates,
itemTracker. You might also see
.push()referred to as a destructive array method since it changes the initial array.
If you’re looking for a method that will mutate an array by adding elements to it, then
.push() is the method for you!
Add two elements to the
chores array using
console.log to print your
chores array to make sure your items were added.