UncleCoder.com

UncleCoder.com

Free programming examples and instructions

adding item to array

Demo and code for adding an item to array by array.push() method. On the top of an array

by Athil


Posted on 08 Jul 2017 Category: Javascript Views: 194

Edited on 30 Jul 2017



DEMO

Hi, here I am going to show how to add an item to an array using push() method By using push method we can add an item to the top position of array.

Syntax of push()

array_name.push(<value>);

Example of adding value to array using push()

var arrayname= ["Car", "Bike", "Truck", "Scooter"];
arrayname.push("Train");

 

Demo Code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Demo for array push javascript</title>
</head>
    <button onclick="addtoarray();">Add to array</button>
<body>
    <h1>Demo for array push javascript </h1>
    <script type="text/javascript">

        function addtoarray()
        {
            var arrayname = ["Car", "Bike", "Truck", "Scooter"];
            arrayname.push("Train");
            alert(JSON.stringify(arrayname));
        }
        </script>
</body>
</html>

Related Posts

Adding item to an array by index

Remove items from array



Leave a Comment:


Advertisement