JavaScriptEasy
How do you add, remove, and update elements in an array?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
To add elements to an array, you can use methods like push, unshift, or splice. To remove elements, you can use pop, shift, or splice. To update elements, you can directly access the array index and assign a new value.
js
let arr = [1, 2, 3];
// Add elements
arr.push(4); // [1, 2, 3, 4]
arr.unshift(0); // [0, 1, 2, 3, 4]
arr.splice(2, 0, 1.5); // [0, 1, 1.5, 2, 3, 4]
// Remove elements
arr.pop(); // [0, 1, 1.5, 2, 3]
arr.shift(); // [1, 1.5, 2, 3]
arr.splice(1, 1); // [1, 2, 3]
// Update elements
arr[1] = 5; // [1, 5, 3]
console.log(arr); // Final state: [1, 5, 3]
Note: If you try to console.log(arr) after each operation in some environments (like Chrome DevTools), you may only see the final state of arr. This happens because the console sometimes keeps a live reference to the array instead of logging its state at the exact moment. To see intermediate states properly, store snapshots using console.log([...arr]) or print values immediately after each operation.
