Array.push()

In our review of Array.indexOf() we looked at using the check for -1 assigned to the indexOf() as an indication that that item was not part of the array.

With Array.push() we can add items to the array.

var family = ['Justin','Laura','Noah','Darcey'];

if(family.indexOf('Waffle the wonder dog') === -1) {
 // Waffle isn't there, let's add him
     family.push('Waffle the wonder dog');
     console.log(family);
} else {
  // Waffle exists already.
}

Leave a comment

Your email address will not be published. Required fields are marked *