I create many new page elements (in some arbitrary hierarchy) using document.createElement and have been using element.removeChild() to erase elements I no longer want. I was wondering if this would correctly clean up all of the sub-elements as well, or if I should be using some sort of recursive function. Javascript uses a garbage collector, so I shouldn’t need to worry about this, right?
I create many new page elements (in some arbitrary hierarchy) using document.createElement and have
Share
Using
element.removeChild(childElement)will remove the nodes from your document tree. If you’ve got a reference to the element, however, the element will not be garbage-collected (GC).Consider:
Example 1 (horrible practice):
Example 2 (bad practice):
Example 3 (good practice):