

The new object inserts at the specified index. This method adds or removes the elements/objects at the specific index defined in it. It is a special type of function which is used for both adding and removing the elements from a specific index in an array. The splice() function is another built-in array method of JavaScript. You will see that the Titanic is successfully added at the end to the array in the below output. We have provided Titanic as a new element value and clicked on the Add Element button. There is an input field to take input from the user and an Add Element button to submit the input value. Enter the movie name and click the button to add it to the array īy executing the above code, you will initially get the output having three movie names. Var movie = document.getElementById("mv").value In this example, we will take the input from the user through HTML form to insert the element using the push() method. In both the above examples, you have noticed that the elements are inserted at the end of the array. So, when you click on this Push Elements button, these elements will be added to the array. We have provided three more series names in the JavaScript code to add in that array. We have also created a Push Element button to add the new element to the array. Click the button to add three more elements to the array īy executing the above code, the response will be the same as given below.


SeriesList.push(" Avengers", " Prison Break", " The Spy") ĭocument.getElementById("result").innerHTML = " Array after adding new elements: " + seriesList Var seriesList = ĭocument.write(" Initially elements in array: " + seriesList + "")
