HTML DOM Element hasChildNodes()

Example

Does an element have child nodes:

let answer = element.hasChildNodes();
Try it Yourself »

Remove the first child node of an element:

if (element.hasChildNodes()) {
  element.removeChild(element.childNodes[0]);
}
Try it Yourself »

Description

The hasChildNodes() method returns true if the specified node has any child nodes, otherwise false.

The hasChildNodes() method is read-only.

HTML Nodes vs Elements

In the HTML DOM (Document Object Model), an HTML document is a collection of nodes with (or without) child nodes.

Nodes are element nodes, text nodes, and comment nodes.

Whitespace between elements are also text nodes.

Elements are only element nodes.


childNodes vs children

childNodes returns child nodes (element nodes, text nodes, and comment nodes).

children returns child elements (not text and comment nodes).


Siblings vs Element Siblings

Siblings are "brothers" and "sisters".

Siblings are nodes with the same parent (in the same childNodes list).

Element Siblings are elements with the same parent (in the same children list).



Syntax

element.hasChildNodes()

Parameters

NONE

Return Value

Type Description
Booleantrue if the element has child nodes, otherwise false.

Browser Support

element.hasChildNodes() is a DOM Level 1 (1998) feature.

It is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 9-11

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.