Press "Enter" to skip to content

Add new items to array – JavaScript

Add new items to array – JavaScript

This article shows how to add new items to an array. To add new items to an array, you need to use .push() method. The push() method adds new elements to the end of an array. Below is the syntax to use .push() method.

array.push(item1,item2, ..., itemX)

Sample implementation

var emptyArray = [];
 function fillEmptyArray(){
 var fillmeString = "Split me and fill the empty array";
 var fillmeArray = fillmeString.split(' ');
 for(var i=0;i<=fillmeArray.length;i++){
 emptyArray.push(fillmeArray[i]);
 }
 alert(emptyArray);
 }
 fillEmptyArray();