Welcome to the Treehouse Community
Looking to learn something new?
olu adesina23,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
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 ChappelleCourses 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