In this section, you will learn how to add an element to Array. In the given example you can see that we have created an array object and added the elements by providing index number to the object.
JavaScript Add Element to Array
In the given JavaScript example, we have created an array object and then added the elements to it by providing index number to the object. This number starts from Zero (0) because whenever we assign any value to array it starts it's counting from 0 only. if an array is having the number 1(one) that means it is the second array in the given list of arrays.
So, while going through the example you will learn about how to add element to an array using JavaScript.
Here is the code:
<html> <head> <h2><u>Add Element to Array</u></h2> <script> var array = new Array(); array[0] = "Java"; array[1] = "ASP.NET"; array[2] = "JavaScript"; array[3] = "PERL"; array[4] = "C/C++"; document.write("<b>The Array is:</b> <br>"); for (i=0;i<array.length;i++) { document.write(array[i] + "<br >"); } </script> </head> <body bgcolor="violet"> </html> |
Output will be displayed as: