We change arrays to add, remove, or update items. This helps keep our list of things up to date.
Modifying arrays in Javascript
const array = [1, 2, 3]; // Add item at the end array.push(newItem); // Remove last item array.pop(); // Add item at the start array.unshift(newItem); // Remove first item array.shift(); // Change item at index array[index] = newValue; // Remove item at index array.splice(index, 1); // Insert item at index array.splice(index, 0, newItem);
Array indexes start at 0, so the first item is at index 0.
splice() can add or remove items anywhere in the array.
const fruits = ['apple', 'banana']; fruits.push('orange'); console.log(fruits);
const numbers = [10]; numbers.pop(); console.log(numbers);
const colors = []; colors.unshift('red'); console.log(colors);
const letters = ['a', 'b', 'c']; letters.splice(1, 1, 'x'); console.log(letters);
This program shows adding, removing, changing, and inserting tasks in an array.
const tasks = ['wash dishes', 'do laundry', 'buy groceries']; console.log('Before modification:', tasks); // Add a new task at the end const newTask = 'call mom'; tasks.push(newTask); // Remove the first task const removedTask = tasks.shift(); // Change the second task tasks[1] = 'fold clothes'; // Insert a task at index 1 tasks.splice(1, 0, 'clean room'); console.log('After modification:', tasks); console.log('Removed task:', removedTask);
Adding or removing items at the end with push/pop is very fast (constant time).
Using splice to add or remove items in the middle can be slower because items need to move.
Remember array indexes start at 0, so be careful when choosing the index.
Use push/pop for stack-like behavior, shift/unshift for queue-like behavior, and splice for precise control.
Arrays can be changed by adding, removing, or updating items.
Use push/pop for the end, unshift/shift for the start, and splice for anywhere.
Always check your indexes to avoid errors.
Practice
Solution
Step 1: Understand array methods for adding elements
push()adds elements to the end,unshift()adds to the start.Step 2: Identify the method that adds to the end
push()is the method that adds elements at the end of the array.Final Answer:
push() -> Option AQuick Check:
Adding at end = push() [OK]
- Confusing push() with pop()
- Using shift() to add elements
- Thinking unshift() adds at end
arr?Solution
Step 1: Recall methods that remove elements
pop()removes from end,shift()removes from start.Step 2: Identify method to remove first element
shift()removes the first element of the array.Final Answer:
arr.shift() -> Option BQuick Check:
Remove first element = shift() [OK]
- Using pop() to remove first element
- Using unshift() which adds elements
- Confusing push() with removal methods
let arr = [1, 2, 3, 4]; arr.splice(1, 2, 8, 9); console.log(arr);
Solution
Step 1: Understand splice parameters
splice(1, 2, 8, 9)means start at index 1, remove 2 elements, then insert 8 and 9.Step 2: Apply splice on array
Original array: [1, 2, 3, 4]. Remove elements at index 1 and 2 (2 and 3), then insert 8 and 9 at index 1. Result: [1, 8, 9, 4].Final Answer:
[1, 8, 9, 4] -> Option AQuick Check:
splice removes and inserts = [1, 8, 9, 4] [OK]
- Confusing splice with slice
- Not removing correct elements
- Forgetting splice inserts new items
let arr = [1, 2, 3, 4]; arr.splice(3, 1); console.log(arr);
Solution
Step 1: Identify the element to remove and its index
The element '3' is at index 2 in the array [1, 2, 3, 4].Step 2: Check the splice call
The code callsarr.splice(3, 1), which removes the element at index 3 (which is 4), not 3.Final Answer:
The index 3 removes the wrong element, should be 2 -> Option CQuick Check:
Remove '3' at index 2, not 3 [OK]
- Using wrong index for splice
- Confusing pop() with splice()
- Thinking splice can't remove elements
arr = [5, 10, 15, 20]. You want to replace the middle two elements with 12 and 18 using a single method call. Which code correctly does this?Solution
Step 1: Identify indexes of middle two elements
Array is [5, 10, 15, 20]. Middle two elements are at indexes 1 and 2 (10 and 15).Step 2: Use splice to replace these elements
Callarr.splice(1, 2, 12, 18)to start at index 1, remove 2 elements, and insert 12 and 18.Final Answer:
arr.splice(1, 2, 12, 18); -> Option DQuick Check:
Replace middle two with splice(1,2,12,18) [OK]
- Wrong start index
- Removing too few or too many elements
- Using splice with wrong parameters
Ready to prove it?
Pass the test and this pattern moves unknown
