Concatenate an array in JavaScript

I’ve been slacking off on the food-related posts lately because of my day job. Then it occurred to me that I started this blog as a general repository of things that interest me, even non-food things. I read a recent article about how productive StackOverflow has made developers because they no longer have to RTFM. I know I have found a lot of value in googling bugs in my code.  In an effort to contribute to the interweb knowledge base, I’m going to try to post a couple of short blogs each week with tips I’ve learned while developing, no matter how elementary they could be.  Who knows, maybe someone else will save some time by stumbling on these posts.

Recently, I’ve been needing to concatenate arrays in JavaScript.  Using the concat() method is tricky because concatenating one array into the other creates a new array instead of altering the original array:

 

The above isn’t exactly what I wanted, so I’ve been doing this instead, which lets me concat an array into the first array, altering the first array:

 

Related Posts with Thumbnails

Leave a Reply

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