Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a nodelist in JavaScript

Understanding the Concept of NodeList

To begin with, imagine you're in a library full of books. You want to find books written by a particular author, say, "J.K. Rowling." After some search, you find a couple of books written by her. These books are just like a NodeList in JavaScript. They are a collection of nodes or elements, just like the books by the same author are a collection.

In technical terms, a NodeList is a group of nodes extracted from the Document Object Model (DOM). It's like a snapshot of the DOM tree at a particular moment. However, don't let the term "NodeList" intimidate you; it's easier to understand than it sounds.

How NodeLists Are Created

You can create a NodeList in JavaScript by using methods like querySelectorAll(), getElementsByTagName(), or getElementsByClassName(). In essence, these methods take a parameter and return all the elements that match the parameter.

Let's look at this through an example:

let divs = document.querySelectorAll('div');

In this code, we are collecting all the "div" elements in the DOM and storing them in the constant "divs". This creates a NodeList.

Diving Deeper: The Nature of NodeLists

Now you might be thinking, "Well, this sounds a lot like arrays." And you're not entirely wrong. A NodeList is similar to an array in that it is an ordered list of nodes. However, it is not an actual array.

Think of it like this: all squares are rectangles, but not all rectangles are squares. Similarly, all arrays are NodeLists, but not all NodeLists are arrays. This is where the difference comes in.

Unlike arrays, NodeLists have a limited method set. While arrays in JavaScript have a plethora of methods like push(), pop(), splice(), slice(), etc., NodeLists only have a limited number of methods available to them.

The Limited NodeList Methods

Even though NodeLists don't have the abundance of methods that arrays do, they do have some that are quite useful. The most commonly used ones are item() and length.

The length property gives you the number of nodes present in the NodeList, and the item() method returns the node at the specified index in the NodeList.

Here's how you can use them:

let divs = document.querySelectorAll('div');

console.log(divs.length); // prints the number of divs

console.log(divs.item(0)); // prints the first div

In this example, divs.length returns the total number of "div" elements in the DOM, and divs.item(0) returns the first "div" element.

NodeList and Live Collection

Another unique feature of NodeLists is that they can be either live or static.

Think of a NodeList as a live radio broadcast. If you tune in late, you will catch the broadcast from the point you tuned in. You won't get the full broadcast from the beginning. This is what a live NodeList is like.

A live NodeList automatically updates to reflect changes in the DOM. So if you add or remove nodes that should be in the NodeList, it will update automatically.

On the other hand, a static NodeList, like the one returned by querySelectorAll(), does not automatically update. It’s a snapshot of the nodes at the moment the NodeList was created.

Here's an example to illustrate this:

let divsLive = document.getElementsByTagName('div');
let divsStatic = document.querySelectorAll('div');

// At this point, both divsLive and divsStatic contain the same nodes

// Now we add a new div to the DOM
let newDiv = document.createElement('div');
document.body.appendChild(newDiv);

// divsLive now includes the new div, but divsStatic does not

Conclusion

In this digital library we call JavaScript, NodeLists serve as our robust librarians, helping us group and locate the specific books (aka nodes) we need. While they might not have all the fancy tools that arrays do, their simplicity and live-update feature make them quite handy. As we continue delving into the vast world of JavaScript, understanding these librarians and how they operate becomes crucial in shaping our journey as proficient developers. So, the next time you see a NodeList, remember, it's just your friendly JavaScript librarian. Happy coding!