How to use Push in JavaScript

In this article we will discuss about How to use Push() Method in JavaScript.
  • 2094

This is the most popular method of the Array object. It is used to insert the given element in the last position in the Array Object and returns the length the length of the Array Object. The Syntax of this method is as follows:

Syntax:

array.push(element1,element 2,.........);

Here element1,element2........ are the elements to be inserted at the last of the Array object.

JavaScript Code:

<html>

<head>

    <title>JavaScript Array push Method</title>

</head>

<body>

    <script type="text/javascript">

        var elements = new Array(1, 2, 3);

 

        var len = elements.push(4);

        document.write("new numbers is : " + elements);

 

        len = elements.push(5);

        document.write("<br />new numbers is : " + elements);

    </script>

</body>

</html>

Output:
myoutput3.jpg

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.