Push-2

 

In this example, We have an array of vegetables and JavaScript push function add "tomato" vegetable at the end of array. See the code snippet:

 
 
 
<!DOCTYPE html> <html> <head> <title>Welcome to LearnKode - A code learning platform</title> </head> <body> <button onclick="addElement()">Add new element</button> <p id="sample"></p> <script> var veg = ["beans", "mushroom", "potato", "brinjal"]; document.getElementById("sample").innerHTML = veg ; function addElement() { veg.push("tomato"); document.getElementById("sample").innerHTML = veg ; } </script> </body> </html>
Output