Loop iteration of nodes in JavaScript

As you should already know, the property children contains all DOM elements that are direct children of this element. There is a similar property childNodes which contains all child nodes of an element.

Let's, for example, use this property to display various nodes from our element:

console.log(elem.childNodes[0]); console.log(elem.childNodes[1]); console.log(elem.childNodes[2]);

And now let's iterate over the nodes of the element in a loop:

for (let node of elem.childNodes) { console.log(node); }

Given a div:

<div id="elem">txt<b>tag</b><!--com-->txt<b>tag</b><!--com--></div>

Loop through all the nodes of this div and print them to the console.

enru