Javascript – prepend item to array

Javascript code to prepend an item at the beginning of array using arr.unshift()

Prepend one value to array

<script type="text/javascript">
var arr = ["v1", "v2"];
arr.unshift("newitem");
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