Last Updated : Jan 1, 2023
For this, we can use these two methods, first one is using array.splice() and other is using for() loop.
Now, in this article we are going to understand array.splice(). So, let us understand how to do this.
As, array.splice() is used to say remove or insert items inside array. The array.splice() method takes three parameters.
The first one is the index where we want to insert or remove array, the next one the number of items which we want to delete and last one is for the number of items which we want to insert inside array.
So, let us understand these with help of codes.
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
- Here, as we can see that we create three paragraphs with one button. The first paragraph shows original array, second shows value to insert into array. And last one shows final array or resulted array.
- Here, inside function we create an array, we choose index, and new value to insert. We directly set value inside array using array.splice() method. And print that array on button click.
- At last, the <body> and <html> tags are closed with </body> and </html> respectively.