Adding and Removing Elements
JS · Arrayssyntax
arr.push(item) // end
arr.pop() // end
arr.unshift(item) // start
arr.shift() // start
arr.splice(index, deleteCount, ...items)example
const tasks = ["code", "test"];
tasks.push("deploy"); // ["code", "test", "deploy"]
tasks.unshift("plan"); // ["plan", "code", "test", "deploy"]
const removed = tasks.pop(); // "deploy"
// Insert at index 2
tasks.splice(2, 0, "review");
console.log(tasks); // ["plan", "code", "review", "test"]Note push/pop are O(1). unshift/shift are O(n) because all indices must be renumbered. splice() mutates the original array.