DEV Community

Himanshu Gupta
Himanshu Gupta

Posted on

2

What is the Difference Between children and childNodes?

What’s the difference between the ParentNode.children and Node.childNodes properties of an element?



var elem = document.querySelector('#my-element');
elem.children;
elem.childNodes;

Enter fullscreen mode Exit fullscreen mode




children

-> each element has that property,
-> is provided by Element class (that class implements Node interface),
-> contains only child elements (it has HTMLCollection type).
Hint: elements are:

, , , , , , etc.

childNodes

-> each node and each element have that property,
-> is provided by Node interface,
-> contains all child nodes (it has NodeList type),
Hint: nodes are: elements, texts and comments.

Image description

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay