Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRu Song
Front End Web Development Techdegree Graduate 13,845 PointsWhy I need to reference ul again?
the ul has already been referenced in the global scope, but why we need to do it again:
ul.addEventListener('click', (e) => {
if (e.target.tagName === 'BUTTON') {
const li = e.target.parentNode;
const ul = li.parentNode; <------- ??
ul.removeChild(li)
2 Answers
Steven Parker
231,261 PointsThis "ul" is being located by traversing from the element that triggered the event. So if the same event handler were to be used with multiple lists, it would always select the correct one.
This might not seem necessary if there is only one list on the page, but it demonstrates "good practice" programming that would be capable of performing properly with more complex pages.
Leanne He
Full Stack JavaScript Techdegree Student 6,779 PointsThanks, same here! I have another, related question: Does this mean that the declaration of the variable ul within the function here overrules the variable ul as defined in the global scope? Is this always the case?
Steven Parker
231,261 PointsYes, and this is known as "shadowing". The global variable continues to exist and remains unchanged, but it cannot be accessed from within the function because of the new one with the same name.
Ru Song
Front End Web Development Techdegree Graduate 13,845 PointsRu Song
Front End Web Development Techdegree Graduate 13,845 PointsThank you for the explanation. Steven Parker
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 PointsDoron Geyer
Full Stack JavaScript Techdegree Student 13,897 Pointsthanks, just had the very same question come to mind.