Matthew C.
—You want to add an item or multiple items to an array. How do you do this?
There are several methods you can use to add an item or multiple items to an array. Let’s take a look at a few of the more common ways to do it.
push()
To add one or more elements to the end of an array, you can use the push()
method:
const arr = ["Mexico", "India"]; // add one element arr.push("China"); console.log(arr); // ["Mexico", "India", "China"] // add multiple elements arr.push("Sudan", "Kenya"); console.log(arr); // ["Mexico", "India", "China", "Sudan", "Kenya"]
This method takes in elements to add to the array as arguments, changes the original array, and returns the new length of the array.
You can append one or more items to an array using spread syntax:
const arr1 = ["Mexico", "India"]; const arr2 = ["Japan", "Brazil"]; // add one item const arrNew1 = [...arr1, "China"]; console.log(arrNew1); // ["Mexico", "India", "China"] // add multiple items const arrNew2 = [...arr1, ...arr2]; console.log(arrNew2); // ["Mexico", "India", "Japan", "Brazil"]
A new array with the appended item or items is created by spreading an array of items into or adding items to an array literal.
You can also use spread syntax to spread an iterable, such as an array, into the arguments of the push()
method:
const arr1 = ["Argentina", "France"]; const arr2 = ["South Korea", "Italy"]; arr1.push(...arr2); console.log(arr1); // ["Argentina", "France", "South Korea", "Italy"]
concat()
You can use the concat()
method to merge two or more arrays. This method does not change the original array but returns a new array. You can use concat()
to append an array of items to another array:
const arr1 = ["Argentina", "France"]; const arr2 = ["South Korea", "Italy"]; const newArr = arr1.concat(arr2); console.log(newArr); // ["Argentina", "France", "South Korea", "Italy"]
length
PropertyYou can also add an item to the end of an array by setting the array element at the index equal to the array’s length:
const arr = ["Norway", "Namibia"]; arr[arr.length] = "New Zealand"; console.log(arr); // ["Norway", "Namibia", "New Zealand"]
This method modifies the original array.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.