JavaScript Array push() method : The push() method is used to add new element in the array. The push() method add the new element at the end of the array. The push() method returns the new length of the array.
Syntax of Array push() method :
array.push(item1, item2, ..., itemX)
In this method you can pass one or more than one elements as a parameter and those parameters added to the end of the array.
Example of Array push() method : Here in this example I will show you how we can add new element into the JavaScript array.
<!DOCTYPE html>
<html>
<body>
<p>To add new element in the array, click the button "add new element".</p>
<button onclick="addElement()">add new element</button>
<p id="container"></p>
<script>
function addElement() {
var students = ["Ashok", "Nitin Kumar", "Rajesh", "Pankaj", "Sumit", "Manish", "Rajesh", "Pramod"];
var result = students.push("Arun");
document.getElementById("container").innerHTML = students;
}
</script>
</body>
</html>
Output :
Ashok, Nitin Kumar, Rajesh, Pankaj, Sumit, Manish, Rajesh, Pramod, Arun
0 Comment(s)