Add element in javascript array

<button onclick="pushElement()">Push Element</button>

<p id="demo"></p>

<script>
var courses = ["javascript", "php", "angularjs", "node js"];
document.getElementById("demo").innerHTML = courses;

function pushElement() {
var pushed_element = courses.push("Mongo DB");
console.log(pushed_element);
document.getElementById("demo").innerHTML = courses;
}
</script>

  • push() will add an element into array and returns the index of that added element.

Share This:

Leave a Reply

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