Surf the Dream A discourse of links and articles from Justin Avery

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 Reply

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

Surf the Dream is a blog that has been running since the mid 2000's when it started on BlogSpot. Over the years it's been rebranded as justinavery.me (now my resume) and JaveryDesign.com (which now redirects back to this site).

I offer consultation services through Simple Things, produce a range of high quality pocket notebooks(including a Solar System Notebook, Space Notebook, and a Guitar Notebook), write about the Universe and run a responsive web design knowledge hub and a RWD Weekly Newsletter.