javaScript Push Array Into Array

javaScript push array into array; Through this tutorial, i am going to show you how to push or add one array to another array in javaScript with examples.

javaScript Push Element and Array Into Array

You can use javaScript push() method to push or add new elements or array to the end of an array.

Note that::- This method changes the length of the given array.

Here, i will take some example for explanation of how to push or add array or array elements into array in javaScript; as shown below:

  • javaScript push elements of array into array
  • JavaScript push multiple items into array
  • javaScript push array into array

javaScript push elements of array into array

Here, i will take simple example for add or push elements of array into array in javaScript; as shown below:

Let you have an array that’s the name arrNum, and it contains four elements inside it, see below:

var arrNum = [
   "one",
   "two",
   "three",
   "four"
 ];

And you want to add or push elements of array into into the arryNum array. So you can use the push() method of javaScript like below:

var arrNum = [
   "one",
   "two",
   "three",
   "four"
 ];

 arrNum.push("five");

 console.log( arrNum );

Output of the above example:

["one", "two", "three", "four", "five"]

JavaScript push multiple items into array

Here, i will take second example for add or push multiple items into array in javaScript.

Let you have an array that names arrMul and it contains five value and want to add or push five more or N elements/items into array, so you can see the the following example:

 var arrMul = [
   "one",
   "two",
   "three",
   "four"
 ]; 

 arrMul.push("six","seven","eight","nine","ten");

 console.log( arrMul );

Output of the above example:

["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]

javaScript push array into array

Here, i will take third example for add the elements of one array to another array in JavaScript.

Let , you have two arrays, a first array name is arryFirst and it contains five items in it. And want to merge second array values to the first array. So you can use the Concat() function of JavaScript.

 var arryFirst = [   "one",   "two",   "three",   "four",   "five" ];

 var arrySecond = [   "six",   "seven",   "eight",   "nine",   "ten" ];

 arryFirst = arryFirst.concat(arrySecond); 

 console.log(arryFirst);

The result of the above example is:

 ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"] 

Conclusion

javaScript push elements and array elements into array; In this tutorial, you have learned, how to add single and multiple items into a given array. And Also learn how to add one array to another array using a concat() method of JavaScript.

More JavaScript Tutorials

Be First to Comment

Leave a Reply

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