In this example, How to add an element in javaScript array? Declare array: var vehicles= new Array("Saab", "Volvo", "BMW"); Add element to array: vehicles.push("Audi"); See the code snippet:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="addVehicle()">Add the array element</button>
<p id="sample"></p>
<script>
var vehicles= new Array("Saab", "Volvo", "BMW");
document.getElementById("sample").innerHTML = vehicles;
function addVehicle()
{
vehicles.push("Audi");
document.getElementById("sample").innerHTML=vehicles;
}
</script>
</body>
</html>