Learn how array indexing works and how to traverse, insert, and delete elements in JavaScript and Python.
An array keeps values in an ordered sequence. Each value is an element, and its numbered position is an index. Arrays are useful when you need to keep a collection in order and read an element by its position.
Values: ["red", "green", "blue"]
Indexes: 0 1 2Indexes start at 0, so the first element is at index 0 and the last element of a collection with n elements is at index n - 1.
A traditional fixed-size array reserves adjacent memory slots for elements of one type. Knowing the starting address and an index lets a program locate an element directly.
JavaScript arrays and Python lists are the growable collections used in this lesson. They support the same indexed operations, but they can also grow, shrink, and contain values of different types. Their implementations manage storage for you; do not assume the values themselves occupy one continuous block of memory.
const colors = ["red", "green", "blue"];
console.log(colors[1]); // "green"Reading colors[1] is O(1) time: the collection can go directly to that index, so the work does not grow with the number of elements. If you know only a value and need to find its index, an unsorted collection may require checking all n elements. That search is O(n) time in the worst case.
Traversal means visiting elements one at a time. Use an index-based loop when you need each position, or a value-based loop when you only need the elements.
This loop prints each position alongside its value:
const colors = ["red", "green", "blue"];
for (let index = 0; index < colors.length; index++) {
console.log(index, colors[index]);
}
// 0 red
// 1 green
// 2 blueIf the positions are irrelevant, iterate over the elements directly:
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(color);
}
// red
// green
// blueBoth loops visit each of the n elements once, so a complete traversal takes O(n) time. In the indexed loop, each colors[index] read is O(1), but doing that read n times still totals O(n). These examples keep only a loop variable, so they use O(1) extra space.
Insertion adds an element at a chosen index. Adding at the end usually avoids moving existing elements. Adding at the beginning or middle shifts later elements one position to the right to make room.
| Insert at | Before | After | Elements shifted |
|---|---|---|---|
| Beginning | ["B", "C"] | ["A", "B", "C"] | Every existing element |
| End | ["A", "B"] | ["A", "B", "C"] | None |
Index 2 | ["A", "B", "D"] | ["A", "B", "C", "D"] | "D" |
The examples below start from a fresh collection in each case.
const letters = ["B", "C"];
letters.unshift("A");
console.log(letters); // ["A", "B", "C"]Moving all existing elements makes beginning insertion O(n).
const letters = ["A", "B"];
letters.push("C");
console.log(letters); // ["A", "B", "C"]Suppose a dynamic array has four slots, all occupied by A, B, C, and D. In a model that doubles capacity, appending E copies four elements into eight slots and writes E into the fifth. The next three appends fit in spare slots.
One resize takes O(n) work. Across a series of appends, capacity grows geometrically, so the total copying is linear in the number of appends. The average cost over that sequence is constant. This is amortized O(1). Different runtimes choose different growth factors.
To put "C" at index 2, the existing "D" moves one position to the right:
const letters = ["A", "B", "D"];
letters.splice(2, 0, "C");
console.log(letters); // ["A", "B", "C", "D"]Middle insertion can shift many elements, so it takes O(n) time.
Deletion removes an element at a known index. Choose the technique based on whether the remaining elements must keep their order.
Removing "B" at index 1 shifts "C" and "D" left to close the gap:
const letters = ["A", "B", "C", "D"];
letters.splice(1, 1);
console.log(letters); // ["A", "C", "D"]An ordered deletion from the beginning or middle takes O(n) because later elements shift. Removing the last element needs no shift and takes O(1).
If order is irrelevant, replace the element at the target index with the last element, then remove the last slot:
Start: ["A", "B", "C", "D"]
Replace index 1: ["A", "D", "C", "D"]
Remove the last slot: ["A", "D", "C"]const letters = ["A", "B", "C", "D"];
const index = 1;
letters[index] = letters[letters.length - 1];
letters.pop();
console.log(letters); // ["A", "D", "C"]With a valid index already known, this takes O(1) time. If you must search for the value first, finding its index can still take O(n).
Choose based on order
Use ordered deletion when positions matter. Use replacement with the last element only when changing the order is acceptable.
Here n is the number of elements in the collection.
| Operation | Time | Why |
|---|---|---|
| Access by known index | O(1) | Go directly to that position. |
| Traverse the array | O(n) | Visit all n elements. |
| Search by value | O(n) worst case | Check up to n elements. |
| Insert at end | Amortized O(1) | Usually add without shifting; occasional growth copies elements. |
| Insert at beginning or middle | O(n) | Shift later elements right. |
| Delete last element | O(1) | No other elements move. |
| Delete while preserving order | O(n) | Shift later elements left. |
| Delete without preserving order | O(1) | Replace with the last element, then remove it. |
O(1) access when you know an index.O(n).O(n).