Arrays: Order, Indices and Updates

Keep a class list in a meaningful order

A teacher keeps a list of students in presentation order. New students may join, but choosing the second presenter must still refer to a specific position in that current list.

An array is an indexed collection. A JavaScript array can grow and holds entries in order; it is not identical to every fixed-size array type in other languages.

  1. Read a position

    Map zero-based indices to first and second entries.

  2. Update a list

    Append, remove and replace entries with the intended effect.

  3. Preserve a copy

    Distinguish a shared array from a separate shallow copy.

Position selects the current presenter

  1. Index 0

    The first presenter.

    Maya
  2. Index 1

    The second presenter.

    Ravi
  3. Index 2

    An appended presenter.

    Asha
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

An index is a position, not a permanent identifier

Read names[1] for the second item. length is the array’s length; valid populated indices in a dense three-item list are 0 through 2. Changing the order changes which value a position selects.

push appends and pop removes the last entry. Assigning names[1] replaces that entry. These operations mutate the array; const prevents reassigning the variable, not changing its elements.

Before starting, review collections and records. These examples use JavaScript; other languages can use different syntax and rules.

Append a presenter and read the updated list

The second item initially is Ravi. Appending Asha increases the length to three without changing index one.

join combines the names into display text. It does not alter the list or convert it into three separate variables.

JavaScript · local teaching example

const names = ['Maya', 'Ravi'];
names.push('Asha');
console.log(names[1]);
console.log(names.length);
console.log(names.join(', '));

Expected output

Ravi
3
Maya, Ravi, Asha

Removing an entry shifts later positions

splice(start, deleteCount) removes entries from a chosen position. Removing Ravi at index one moves Asha into index one.

Use this deliberately rather than delete, which can leave a hole without shrinking the array. Finding a record by ID is safer when positions are not stable.

JavaScript · local teaching example

const names = ['Maya', 'Ravi', 'Asha'];
names.splice(1, 1);
console.log(names[1]);
console.log(names.length);

Expected output

Asha
2

Create a separate shallow array before editing

slice creates a new array containing the current entries. Replacing a string entry in the copy does not replace it in the original.

A shallow copy still shares any object entries. To isolate nested student records, decide which records need copying too.

JavaScript · local teaching example

const original = ['Maya', 'Ravi'];
const revised = original.slice();
revised[0] = 'Asha';
console.log(original[0]);
console.log(revised[0]);

Expected output

Maya
Asha

Apply the rule to a small change

Remove the last presenter from a three-name list and print both the removed name and the remaining order.

Worked answer

pop returns Asha, the last item, and leaves Maya then Ravi. It is unsuitable for removing the first student in a waiting line.

JavaScript · local teaching example

const names = ['Maya', 'Ravi', 'Asha'];
console.log(names.pop());
console.log(names.join(', '));

Expected output

Asha
Maya, Ravi

Recognize mistakes and limits

Reading an absent array entry gives undefined; it does not prove the value is an empty student record. Avoid out-of-range access and sparse lists when the task expects dense records.

Insertion or removal near the front can move later entries and require more work. JavaScript arrays are flexible objects; do not promise every operation has the same cost as a low-level fixed-size array.

For the language or tool rules used here, see JavaScript indexed collections.

Check the core idea

After removing index one from [Maya, Ravi, Asha], what is at index one?
Choose one answer. You can try again.

Answer and explanation

Asha.

Yes. The later item shifts into that position.

Apply the idea to a new situation

Does slice fully isolate student objects nested in an array?
Choose one answer. You can try again.

Answer and explanation

No, the new array still shares object entries unless they are copied too.

Yes. Separate containers do not imply separate nested objects.

Use array order deliberately

Arrays fit ordered collections. Trace positions after updates, handle missing entries and distinguish a shallow container copy from independent records.

Connect this idea with unique values and lookup by stable identifiers.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Sets: Unique Values and Membership →