Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

This tutorial will help you to add an array to another array in javascript, here we will use the push() method to insert the new array in the array.

JavaScript provides the push() method to add new elements to an existing array, we have to just pass the array 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 an array to another 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 the push() method to add a new array
  3. console array to verify whether 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 array added to the first array.

For now, let’s check the output.

Output

javascript, add array in another array
javascript, add array in another array

All the best đź‘Ť.