Hi Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial will help you to add new items or elements to your array, here we will use the push() method to insert the new element in the array.

JavaScript provides the push() method to add new elements an existing array, we have to just pass elements in the push() method.

The push() method adds new elements at the last index of the array.

Let’s get started with today’s tutorial How to add items to an array in javascript?

Before going to an example, let’s see what we are going to do in code…

  1. Create sample array variable
  2. Use push() method to add new element
  3. console array to verify added or not.

So simple, correct?

// create sampe of array object variable
const sampleArray = [
  { userId: 1, name: "infinitbility", domain: "infinitbility.com" },
console.log("old sampleArray", sampleArray)

// add new object in a sampleArray
sampleArray.push({ userId: 2, name: "aGuideHub", domain: "aguidehub.com" })
console.log("new sampleArray", sampleArray)

Well, when you run the above code, you can able to see a new object added to an array.

For now, let’s check the output.


javascript, add item in a array
javascript, add item in a array

All the best 👍.