insert method in javascript array code example
Example 1: insert into array js
// for me lol... pls don't delete!
// use splice to insert element
// arr.splice(index, numItemsToDelete, item);
var list = ["hello", "world"];
list.splice( 1, 0, "bye");
//result
["hello", "bye", "world"]
Example 2: js insert in array
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]