Home
<!DOCTYPE html> <html> <body> <h1>Demo: Add Array Elements in JavaScript</h1> <p id="p1"></p> <p id="p2"></p> <p id="p3"></p> <p id="p4"></p> <script> let cities = ["Mumbai", "New York", "Paris", "Sydney"]; document.getElementById("p1").innerHTML = cities; cities[4] = "Delhi"; //add new element at last document.getElementById("p2").innerHTML = cities; cities[cities.length] = "London";//use length property to specify last index document.getElementById("p3").innerHTML = cities; cities[9] = "Pune"; document.getElementById("p4").innerHTML = cities; </script> </body> </html>
Result: