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 Using let with for Loops

olu adesina
olu adesina
23,007 Points

Dont understand how the buttons.length is used in this example

when i see the length property be used i expect to see a variable ive not seen it used like this how is it looping through the html

3 Answers

the variable 'buttons' is storing all the instances of <button> on the page using the getElementsByTagName. (Notice the plural 'Elements' in the method name) The buttons.length refers to the number of <button>s inside the buttons variable which in this case is 10.

John Chappelle
PLUS
John Chappelle
Courses Plus Student 551 Points

When the 'button' variable grabs buttons from the DOM, it grabs all of the buttons and stores them to the variable 'buttons' as an array. Because 'buttons' is an array, it can use the .length method to get the length of the array (i.e. the amount of buttons elements stored in 'buttons').

Had the same question. Got the answer now. Thank you