Computer >> Computer tutorials >  >> Programming >> Javascript

How to add an element in the last of a JavaScript array?


To add an element in the last, use the JavaScript push() method. JavaScript array push() method appends the given element(s) in the last of the array and returns the length of the new array.

Example

You can try to run the following code to add an element in the last of the array −

<html>
   <head>
      <title>JavaScript Array push Method</title>
   </head>

   <body>
      <script>
         var numbers = new Array(30, 70, 75, 90);
         var length = numbers.push(100);
         document.write("new numbers are : " + numbers );
         length = numbers.push(200);
         document.write("<br />new numbers are : " + numbers );
     </script>
   </body>

</html>

Output

new numbers are : 30,70,75,90,100
new numbers are : 30,70,75,90,100,200