javaScript Push Element, Array Into Array Example

javaScript Push Element, Array Into Array Example

javaScript push array elements into array; In this tutorial, You will learn how to add single, multiple items(elements) into an array, and how to add one array to another array using the array push() and Concat() method of javaScript with examples.

javaScript Push Element and Array Into Array

javaScript push() Method

The javaScript push() method is used to add new elements to the end of an array. And changes the length of the given array.

javascript push elements and array elements into array tutorial; you will learn the following:

  • How to add the single item of the array?
  • How to add the multiple items of the array?
  • How to push array into array in javaScript?

How to add the single item of the array?

Here, you will learn how to add a single item into a given array. Let’s take an example.

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

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

If you want to add the single item 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 );

The result of the above example is:

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

How to add the multiple items of the array?

If you want to add multiple items and elements into a given array. You can use the below example.

Let’s take a new example of how to add multiple items into a given array.

Suppose we have an array that names arrMul and it contains five value and we want to add five more or N value into it, so use the below example

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

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

 console.log( arrMul );

The result of the above example is:

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

How to push array into an array in javaScript?

Let’s take an example of how to add the items of one array to another array or how to push array into an array in JavaScript.

Suppose, you have two arrays, a first array name is arryFirst and it contains five items in it. And you have a second array name arrySecond and it also contains five items in it. If you 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.

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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