var how_many = document.getElementsByClassName("content"); console.log(how_many); With this code, the object is displayed:
HTMLCollection []
0: div.content
1: div.content
2: div.content
3: div.content
length: 4
proto : HTMLCollection
If we change the code to the following:
var how_many = document.getElementsByClassName("content").length; console.log(how_many); That in the console appears 0.
Even if we summarize:
var how_many = document.getElementsByClassName("content"); console.log(how_many); console.log(how_many.length); That is displayed as that object, and zero.
Why?
HTMLCollectionwhen a new suitable element appears on the page, but if they update, everything is simple: the browser console usually shows the state of the object at the current moment, and not at the time you calledconsole.log. And if yourdiv.contentis notdiv.contentyet by the time you calledconsole.log, then when outputting such a collection it will be0, although if you print the collection itself, then it will have a non-zero length. - selya