I’m taking the JavaScript Certification and am pretty frustrated trying to understand the definitions in the “What Is the querySelectorAll() Method, and When Should You Use It?” lesson.
Fairly straightforward in its first mention:
“querySelectorAll() returns a NodeList object, a collection of nodes that match the specified CSS selector.”
OK. It returns an Object.
But later in the same page, the example says:
const matches = document.querySelectorAll("ul.ingredients li");
This will return a collection of three nodes. Each node represents a list item element:
// NodeList(3)
{
0: `<li>Flour</li>`,
1: `<li>Cheese</li>`,
2: `<li>Water</li>`,
length: 3,
}
You can work with this collection exactly like you would work with any other JavaScript array.
So it’s a collection. But also an array, because “any other JavaScript array” implicitly includes this collection in the definition of “array”. And the structure looks like what I’d use to declare an object (a non-array object).
The quiz at the end says the querySelectorAll() does not return an array. The correct answer, NodeList, well, fair enough. That is defined within the lesson. But it seems incomplete to me. NodeLists are objects, and collections, but not arrays, that can be used exactly like arrays?
Taking '“object” as the superset of collections and arrays clears up that stage, but I do not understand exactly what a collection is, and why an object that works exactly like an array is not an array.
Can anyone point me in the right direction for some definition?