JavaScript childElementCount to Count Direct Child Elements
Use element.childElementCount to read how many direct child elements a DOM element has. It excludes text and comments. Elements nested inside a child are descendants but do not count as direct children.
Count only direct child elements
The example creates two li elements, with a strong inside one. Both childElementCount and children.length report 2. querySelectorAll('*').length includes the nested element and reports 3. DOM Standard · childElementCount
const list = document.createElement('ul');
const first = document.createElement('li');
first.append(document.createElement('strong'));
list.append(first, document.createComment('note'), document.createElement('li'));
console.log(list.childElementCount); // 2
console.log(list.children.length); // 2
console.log(list.querySelectorAll('*').length); // 3
console.log(list.childNodes.length); // 3
Use childNodes.length to include text and comment nodes. Here there are two direct li elements and one comment, so that count is 3. Indented HTML can add whitespace text nodes and increase the count. DOM Standard · childNodes
Read the count after changing the children
childElementCount is read-only. After changing the list with append() or remove(), read it again for the current count. A number saved in a variable is a snapshot. If you display a count, refresh the display after the change.
const list = document.createElement('ul');
list.append(document.createElement('li'));
const before = list.childElementCount;
const item = document.createElement('li');
list.append(item);
console.log(before, list.childElementCount); // 1, 2
item.remove();
console.log(list.childElementCount); // 1
Add and remove list items in a complete example
Save the code as child-element-count.html in UTF-8 and open it in a browser. It starts with 2 direct child elements and 3 descendants. Adding an item changes the numbers to 3 and 4. Removing the last item takes them back to 2 and 3.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Count child elements</title>
</head>
<body>
<ul id="items">
<li>Item 1 <strong>Nested emphasis</strong></li>
<li>Item 2</li>
</ul>
<button id="add" type="button">Add item</button>
<button id="remove" type="button">Remove last item</button>
<p id="count" aria-live="polite"></p>
<script>
const list = document.querySelector('#items');
const count = document.querySelector('#count');
let nextNumber = 3;
function update() {
const descendants = list.querySelectorAll('*').length;
count.textContent = `Direct child elements: ${list.childElementCount} · All descendant elements: ${descendants}`;
}
document.querySelector('#add').addEventListener('click', () => {
const item = document.createElement('li');
item.textContent = `Item ${nextNumber++}`;
list.append(item);
update();
});
document.querySelector('#remove').addEventListener('click', () => {
list.lastElementChild?.remove();
update();
});
update();
</script>
</body>
</html>
Remove every item to see both counts reach 0. For an empty list, lastElementChild is null, so ?.remove() runs only when there is an element to remove.




