JavaScript Element childElementCount Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance property

What You’ll Learn

Element.childElementCount is a read-only instance property that returns how many element children a node has. Learn how it ignores text nodes, how it compares to childNodes.length, and how to use it for empty checks—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read-only

03

Type

Number

04

Counts

Element children

05

Status

Baseline widely

06

Same as

children.length

Introduction

A container like a sidebar or list often needs a quick answer: “Does this have any child elements?” You do not always need the full list—just the count.

childElementCount returns that number. It counts only elements (tags), not text or comments sitting between them.

JavaScript
const sidebar = document.getElementById("sidebar");
if (sidebar.childElementCount > 0) {
  // Do something
}
💡
Beginner tip

Pretty-printed HTML often inserts whitespace text nodes. Those inflate childNodes.length but leave childElementCount unchanged.

Understanding the Property

MDN: the Element.childElementCount read-only property returns the number of child elements of this element.

  • Read-only — change the DOM to change the count.
  • Elements only — ignores text and comment nodes.
  • Matches children.length — same element-only view.
  • Baseline — widely available since July 2015 (MDN).

📝 Syntax

JavaScript
childElementCount

Value

A number: how many child elements this element currently has.

ItemDetail
Typenumber (unsigned long)
AccessRead-only
CountsElement children only
Relatedchildren, childNodes, firstElementChild
⚠️
Elements vs nodes

childElementCount === children.length, but it is often smaller than childNodes.length when whitespace text nodes exist.

📋 MDN Sidebar Example Shape

MDN checks whether a sidebar has any element children before doing work:

JavaScript
<aside id="sidebar">
  <a href="#">Home</a>
  <a href="#">Docs</a>
</aside>
JavaScript
let sidebar = document.getElementById("sidebar");
if (sidebar.childElementCount > 0) {
  console.log("Sidebar has", sidebar.childElementCount, "links");
}

Related learning: children, firstElementChild, lastElementChild, and childNodes.

⚡ Quick Reference

GoalCode / note
Read countel.childElementCount
Has children?el.childElementCount > 0
Same ideael.children.length
All nodesel.childNodes.length (includes text)
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.childElementCount.

Kind
get only

Instance

Type
number

Element count

Ignores
text

And comments

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: childElementCount. Labs use a simple sidebar container so you can count element children safely.

📚 Getting Started

Read the count and follow MDN’s empty check.

Example 1 — Read childElementCount

Log how many element children a sidebar has.

JavaScript
const sidebar = document.getElementById("sidebar");
console.log(sidebar.childElementCount);
Try It Yourself

How It Works

Each nested tag (<a>, <div>, …) counts as one. Text between tags does not.

Example 2 — MDN Check > 0

MDN: only run logic when the container has element children.

JavaScript
let sidebar = document.getElementById("sidebar");
if (sidebar.childElementCount > 0) {
  console.log("ready");
} else {
  console.log("empty");
}
Try It Yourself

How It Works

This pattern is perfect for empty states, badge counts, and “show list vs placeholder” UI.

📈 Compare Counts, Live Updates & Snapshot

See why text nodes matter, then watch the count change.

Example 3 — vs childNodes.length

Whitespace text nodes make childNodes larger.

JavaScript
const box = document.getElementById("box");
console.log({
  childElementCount: box.childElementCount,
  childrenLength: box.children.length,
  childNodesLength: box.childNodes.length
});
Try It Yourself

How It Works

Prefer childElementCount (or children.length) when you care about tags, not formatting whitespace.

Example 4 — Live Update After Append

Adding an element child increases the count immediately.

JavaScript
const list = document.createElement("ul");
console.log(list.childElementCount); // 0

const item = document.createElement("li");
item.textContent = "One";
list.appendChild(item);
console.log(list.childElementCount); // 1
Try It Yourself

How It Works

The property always reflects the current DOM. Removing children lowers the count the same way.

Example 5 — Support Snapshot

Feature-detect and remember the element-only rule.

JavaScript
console.log({
  supported: "childElementCount" in Element.prototype,
  counts: "element children only",
  tip: "Same idea as children.length — ignores text nodes",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Use this property whenever you need a fast, reliable element tally.

🚀 Common Use Cases

  • Empty-state checks before rendering lists or sidebars.
  • Badge or counter UI (“3 items”) without building an array.
  • Comparing container sizes after DOM updates.
  • Avoiding false “has children” results from whitespace text nodes.
  • Teaching the difference between nodes and elements.

🔧 How It Works

1

Parent has mixed children

Elements, text nodes, and maybe comments.

DOM
2

Property filters to elements

Only tags contribute to the count.

Filter
3

You read a number

Fast check without looping the collection.

Read
4

DOM changes update it

Append or remove children and the count stays accurate.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • Read-only — mutate the DOM to change the value.
  • Equals children.length; usually differs from childNodes.length.
  • Related: attributes, children, EventTarget, JavaScript hub.

Browser Support

Element.childElementCount is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.childElementCount

Read-only number — count of child elements (ignores text/comment nodes).

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Supported (IE9+)
Yes
childElementCount Baseline

Bottom line: Use childElementCount for fast element-only tallies. Prefer it over childNodes.length when whitespace text nodes would confuse empty checks.

Conclusion

childElementCount is the simplest way to ask how many element children a node has. Use it for empty checks, counters, and anywhere whitespace text nodes would mislead childNodes.length.

Continue with children, attributes, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use > 0 for empty-state checks
  • Prefer it when you care about tags only
  • Pair with children when you need the list
  • Re-read after DOM mutations
  • Compare with children.length while learning

❌ Don’t

  • Assign to childElementCount (read-only)
  • Assume it equals childNodes.length
  • Count text nodes with this property
  • Forget nested grandchildren are not direct children
  • Skip null checks on the parent element itself

Key Takeaways

Knowledge Unlocked

Five things to remember about childElementCount

Read-only count of direct element children—ignores text and comments.

5
Core concepts
📝 02

Number

element count

Type
🔍 03

Empty checks

lists & sidebars

Use
04

Baseline

widely available

Status
🎯 05

Not text

ignores whitespace

Tip

❓ Frequently Asked Questions

It returns the number of child elements of this element — only Element nodes, not text or comment nodes.
No. MDN marks Element.childElementCount as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. It is a read-only instance property. Change the count by adding or removing child elements.
childNodes includes text nodes, comments, and elements. childElementCount counts only element children — same idea as children.length.
No. Spaces and newlines between tags create text nodes in childNodes, but they do not increase childElementCount.
Use it to check if a container has any element children, for empty-state UI, or to compare list sizes without looping.
Did you know?

childElementCount comes from the ParentNode mixin—so Document and DocumentFragment also expose it, not only Element.

Next: children

Learn the live HTMLCollection of an element’s child tags.

children →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful