You could use children instead of childNodes
, because of formatting you introduced some text nodes (Yes they are nodes with type 3 not just some whitespace) and childNodes
will return all of them.
console.log(allDivNodes[0].children.length);
Or with childNodes you can loop though and ignore the ones with nodeType === 3
.
Also similarly you have childElementCount as well which will give you the childElement count and will ignore text nodes.