Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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