JavaScript USES the push method to add an element to the end of the array usage instance

  • 2020-05-26 07:49:13
  • OfStack

This example demonstrates the use of JavaScript to add an element to the end of an array using the push method. Share with you for your reference. The details are as follows:

The following code demonstrates the JS array by adding an element to the end of the array using the push method


<!DOCTYPE html>
<html>
<body>
<p id="demo">
Click the button to add a new element to the array.
</p>
<button onclick="myFunction()">Try it</button>
<script>
var fruits = ["Banana","Orange","Apple","Mango"];
function myFunction()
{
fruits.push("ofstack.com")
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>

The above code executes and outputs the following results

Banana,Orange,Apple,Mango,ofstack.com

I hope this article is helpful for you to design javascript program.


Related articles: