for each child element javascript code example

Example: for each child of element

/* HTML */
/*
<ul class="navigation__secondary" id="navigation-bar">
  <li class="navigation__secondary-item">
  	<span>Item</span>
  </li>
  <li class="navigation__secondary-item active">
	  <span>Second item</span>
  </li>
  <li class="navigation__secondary-item active">
  	<span>Third item</span>
  </li>
</ul>
*/

const secondaryNavList = document.querySelector('.navigation__secondary');

/* Method 1 A (ES6) */
const secondaryNavListChildren = Array.from(secondaryNavList.children);

for (let secondaryNavListItem of secondaryNavListChildren) {
  console.log(secondaryNavListItem);
}

/* Method 1 B (ES5) */
const secondaryNavListChildren = Array.prototype.slice.call(secondaryNavList.children);

for (let secondaryNavListItem of secondaryNavListChildren) {
  console.log(secondaryNavListItem);
}

/* Method 2 */

const secondaryNavListChildren = secondaryNavList.childNodes;

secondaryNavListChildren.forEach((navListItem) => {
  console.log(navListItem);
})