js insert code example
Example 1: javascript insert item into array
var colors=["red","blue"];
var index=1;
//insert "white" at index 1
colors.splice(index, 0, "white"); //colors = ["red", "white", "blue"]
Example 2: javascript push in specific index
arr.splice(index, 0, item);
//explanation:
inserts "item" at the specified "index",
deleting 0 other items before it
Example 3: javascript push in position
var arr = [];
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";
console.log(arr.join());
arr.splice(2, 0, "Lene");
console.log(arr.join());
Example 4: js add
//HOW TO ADD:
var num1 = 4;
var num2 = 6;
var answer = 4 + 6; //You can do num1 + num2 here!
function add(num1, num2) {
return num1 + num2;
};
console.log("4 + 6 is: " + add());
console.log("I hope this helped!");
Example 5: js insert
arr.splice(index, 0, item);
// will insert item into arr at the specified index
// (deleting 0 items first, that is, it's just an insert).