Add new items to array – JavaScript

June 27, 2011 by satz  
Filed under JavaScript, Scripts

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();

  • Leave a Comment

    Leave your sincere comments and feed backs below. It helps us to improve the quality and encourages us to do more.

Speak Your Mind

Tell us what you're thinking...
All the support requests shall be posted at forum.tutorials2learn.com