I am JS newbie reading about JS data types which pointed me to the Live NodeLists and Static NodeLists on those pages: Why is getElementsByTagName() faster than querySelectorAll()? and HTMLCollections & NodeLists Mentioned pages describes their differences using very similar examples:
Live NodeLists:
var divs = document.getElementsByTagName("div"),
i=0;
while(i < divs.length){
document.body.appendChild(document.createElement("div"));
i++;
}
Static NodeLists:
var divs = document.querySelectorAll("div"),
i=0;
while(i < divs.length){
document.body.appendChild(document.createElement("div"));
i++;
}
I am interesting in this particular piece of code:
var divs = document.getElementsByTagName("div"),
i=0;
resp. this one:
var divs = document.querySelectorAll("div"),
i=0;
Can somebody please clarify me what is this construction doing? Or the question maybe should be: what is the content and type of variable divs
after this? I was assuming that this is array, so I have tried this minimalistic JS vhere I have added further variables:
var divs = document.getElementsByTagName("div"),
j=5,
i=0;
alert(divs.length);
but no mather how many other variables I have added to divs
along with document.getElementsByTagName
the result of alert()
was always 1. Then I have tried print it via typeof()
and it returns me that this is object. What is going on here, is it some special JS syntactic sugar or what?