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 trial

JavaScript JavaScript and the DOM Getting a Handle on the DOM Select Elements by Class Name

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

For.... of

As, I read from the links from the Teacher's Note, What I understood is, The const highlight's scope gets over once the first loop is finished and then it's like a fresh assignment for the

const highlight

from the highlights Collection. Moreover, we only use the const if it's read only purpose

Is my understanding of the Usage of (const) in the for .. of loop is right?

1 Answer

Julie Branyan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Julie Branyan
Full Stack JavaScript Techdegree Graduate 16,337 Points

Hi Karen -

Your understanding is correct. If you were to add a console.log(highlight), you would see that it loops through the li element 'make coffee' first, then updates the background color to 'cornsilk', then it loops through the next li highlight class and so on until its completes. 'highlight' represents each element in the highlights HTML collection.

The main benefit is to prevent someone from changing the assignment of the identifier in the loop body. It makes it read only. This stackoverflow article example link, the first answer has two code snippets that are worth running. It shows how if you want to change the assignment within a loop body by trimming the string, it wont allow you with const and will throw an error.

If its helpful, I would use console.log() to log out the different items and play with it. This helped me as I was working to understand it better. Hope this helps!