Introduction

The two main ways to append items to an array are push() and concat(). Both methods serve the same purpose but go about their business in different ways.

When you use push(), it mutates the original array. And the concat() method technically doesn't append the item to the array but instead creates an entirely new array with the new item included at the end of the new version.

Let's look at some code examples.

Table Of Contents

Push()

Here's how you would append a single item to an array using the push() method:

    
      const names = ["Billy", "Johnny", "Franky", "Harry"]

      names.push("Barry")

      // ["Billy", "Johnny", "Franky", "Harry", "Barry"]
    
  

And you could do the same thing if you wanted to append multiple items:

    
      const names = ["Billy", "Johnny", "Franky", "Harry"]

      names.push("Barry", "Terry", "Larry")

      // ["Billy", "Johnny", "Franky", "Harry", "Barry", "Terry", "Larry"]
    
  

Concat()

Below is how you'd use concat() method to append an item to the end of an array:

    
      const names = ["Billy", "Johnny", "Franky", "Harry"]

      const newNames = names.concat("Barry")

      // ["Billy", "Johnny", "Franky", "Harry", "Barry"]
    
  

And to do the same thing with multiple items:

    
      const names = ["Billy", "Johnny", "Franky", "Harry"]

      const newNames = names.concat("Barry", "Terry", "Larry")

      // ["Billy", "Johnny", "Franky", "Harry", "Barry", "Terry", "Larry"]
    
  

Notice that the concat() method creates an entirely new array instead of mutating the existing array the way the push() method does.

Thanks for reading and happy coding!