Code Literacy

Unit 6 · JavaScript in the page

Removing a clicked item: pop vs splice vs filter

pop() always removes the last item. To remove the one that was clicked, use its index with splice(index, 1), or build a new array with filter. Then call render().

The mix-up

What people often think

“pop() removes the task that was clicked.”

What the tutor says

pop() always removes the last item. Use the button's index with splice, or filter out that task.

How it works

Patching the visible count instead, by subtracting one from the text, leaves the array unchanged, so the next render brings the task back.

filter is often the clearest: keep every item except the one to remove.

Side by side

The mix-upJavaScript
todos.pop();  // removes the last task, whichever was clicked
Wrong item.
What worksJavaScript
todos.splice(index, 1);
// or
todos = todos.filter((t, i) => i !== index);
render();
Removes the clicked task.

Close cousins of this mix-up

The tutor watches for these in the same lesson.

  • “The fix is to subtract one from the counter text.”

    That patches the symptom, and the array still has three items. Remove the task from todos, then call render().

Practise it with a tutor watching

State

Build a counter and a to-do list where the data decides what the screen shows.

20 minutes See it Build it Break it Say it
Start the lesson

Free with an account. The first two units need no account.

© 2026 webfaCeMEdia. Code Literacy™ is a trademark of webfaCeMEdia.