87. Removing Elements from the DOM
To clean up or update the UI, we often need to remove elements.
1. remove() (Modern)
This is the simplest way. You call the .remove() method directly on the element you want to delete.
javascript const trash = document.getElementById('item-to-delete');
if (trash) { trash.remove(); // The element is gone from the DOM }
2. removeChild() (Legacy/Parent-Based)
This method requires you to have a reference to the parent element and pass the child element as an argument.
javascript const parent = document.getElementById('list'); const child = document.getElementById('first-item');
parent.removeChild(child);
Best Practice: Always use the .remove() method when possible, as it is cleaner and more concise.