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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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