Javascript – append item to array

Javascript code to append one or multiple values at the end of array

Append one value

<script type="text/javascript">
var arr = ["v1", "v2"];
arr.push("v3");
arr.forEach (function(item, idx) {
  document.write("idx=" + idx + " item=" + item  + "<br/>");
});
</script>
refresh

Append multiple values

<script type="text/javascript">
var arr = ["v1", "v2"];
arr.push("v3", "v4");
arr.forEach (function(item, idx) {
  document.write("idx=" + idx + " item=" + item  + "<br/>");
});
</script>
refresh
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments