JavaScript – Document.childElementCount

JavaScript Document.childElementCount property returns the number of child elements of the document.

Syntax

The syntax to read childElementCount property of the document is

</>
Copy
document.childElementCount

Document.childElementCount property is read-only.

Example

In the following example, we read the childElementCount property value of this document and write it to #output. Since, there is only one child element at the root of this document, which is <html>, we get the output as 1.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<body>
  <p id="output"></p>
  <script>
    var output = document.getElementById('output');
    var result = document.childElementCount;
    output.innerHTML = 'Number of Child Elements in Body : ' + result;
  </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we have learnt about Document.childElementCount property, with examples.