DOM nodes and elements
Quick answer
A document is a tree of nodes. Elements are one kind of node; text and comments are nodes too. children lists element children, while childNodes includes text and comments.
Browser example
Run in a browser document, not a bare Node.js process.
const p=document.createElement("p"); p.append("Hello",document.createElement("strong")); console.log(p.childNodes.length,p.children.length);Expected console output:
2 1Common mistake
Do not count childNodes when you need only elements.
Practice
Append a comment and predict both counts.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
DOM nodes and elements — Knowledge Check
Check the central concept, apply it to a new scenario, and predict browser behavior.
Which collection includes text nodes?
A paragraph contains only a text node. How many element children does it have?
You need to count direct child elements in a navigation list. Which property should you read?
A comment is appended to a container. Which count increases by one?
What is printed by this browser snippet? (DOM nodes and elements)
const p=document.createElement("p");p.append("One",document.createComment("note"),document.createElement("em"));console.log(p.childNodes.length,p.children.length);