1//we can add elements to array using splice
2
3const strings=['a','b','c','d']
4
5//go the 2nd index,remove 0 elements and then add 'alien' to it
6strings.splice(2,0,'alien')
7
8console.log(strings) //["a", "b", "alien", "c", "d"]
9
10//what is the time complexity ???
11//worst case is O(n). if we are adding to end of array then O(1)
12//in our example we added to the middle of array so O(n/2)=>O(n)
1var fruits = ["Banana", "Orange", "Apple", "Mango"];
2document.getElementById("demo").innerHTML = fruits;
3
4function myFunction() {
5 fruits.splice(2, 0, "Lemon", "Kiwi");
6 document.getElementById("demo").innerHTML = fruits;
7}