insert values in the array javascript code example

Example 1: how to insert a value into an array javascript

var list = ["foo", "bar"];


list.push("baz");


["foo", "bar", "baz"] // result

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"]