Sometimes, we want to loop through childNodes with JavaScript.
In this article, we’ll look at how to loop through childNodes with JavaScript.
How to loop through childNodes with JavaScript?
To loop through childNodes with JavaScript, we can use a for-of loop.
For instance, we write
const children = element.childNodes;
for (const child of children) {
console.log(child);
}
to get the child nodes of the element with the childNodes property.
Then we loop through the children child nodes with a for-of loop.
We get the node being looped through from child.
Conclusion
To loop through childNodes with JavaScript, we can use a for-of loop.