Tuesday, 12 March 2019

JavaScript Array push() Method

Add a new item to an array:


var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");  

 

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_push

<!DOCTYPE html>
<html>
<body>

<p>Click the button to add a new element to the array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
  fruits.push("Kiwi");
  document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>

No comments:

Post a Comment

zomok E-commerce system plan. Choose your online ordering system. No-risk 30 day free trial. Then USD 9/month. No credit card required.

zomok E-commerce system plan. Choose your online ordering system. No-risk 30 day free trial. Then USD 9/month. No credit card required. h...