Technical skill practice

JavaScript splice: Edit a Schedule and Inspect Removed Items

splice edits its array and returns the elements it removed. Its start and delete-count arguments describe an edit, unlike slice bounds describing an extracted window.

The task

Replace one scheduled activity with two activities, then inspect a nonmutating slice of the revised schedule without repeating the extraction fixture.

Runnable Node 22 example

const assert = require('node:assert/strict');

const schedule = ['briefing', 'tour', 'review'];
const removed = schedule.splice(1, 1, 'setup', 'practice');
assert.deepEqual(removed, ['tour']);
assert.deepEqual(schedule, ['briefing', 'setup', 'practice', 'review']);
const view = schedule.slice(1, 3);
assert.deepEqual(view, ['setup', 'practice']);
assert.equal(schedule.length, 4);
const inserted = schedule.splice(schedule.length, 0, 'close');
assert.deepEqual(inserted, []);
console.log(schedule.join(' -> '));

Expected output and explanation

The first edit starts at index one, removes one activity and inserts two. The returned array contains tour, while schedule now holds four activities. The result of splice is therefore not the edited schedule.

The slice reads the two inserted activities without removing them. The final splice uses a delete count of zero to append close and returns an empty removed-elements array. An empty return can therefore accompany a successful insertion.

Output from the example

briefing -> setup -> practice -> review -> close

Pitfall to check

Passing only a start value to splice removes through the end, while passing a zero delete count inserts without deletion. Make that argument explicit when accidental truncation would be costly. Preserve or copy the source first when its original membership must remain available.

Questions, answered

Can I replace one element without changing the array length?

Yes: remove one and insert one in the same splice call. This example intentionally inserts two so the length change is visible.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.