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

JavascriptWeb DevelopmentFront End Technology

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 −

Live Demo

<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
raja
Published on 12-Feb-2018 16:53:37
Advertisements