Start empty. A new div has no element children → null.
Compare children[0]. That is undefined, not null.
Add only text. Text nodes do not count as element children.
Always null-check before reading tagName or textContent.
Compatibility
Browser Support
Element.firstElementChild is Baseline Widely available (MDN: since July 2015). Safe across modern and older evergreen browsers.
✓ Baseline · Widely available
Element.firstElementChild
Read-only first child Element or null — ParentNode mixin, skips text and comments.
99%Widely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
firstElementChildExcellent
Bottom line: Safe for production. Prefer it over firstChild whenever you specifically need an Element child.
Frequently Asked Questions
The first child Element of the parent, or null if there is no element child. Text and comment nodes are skipped.
No. MDN marks Element.firstElementChild as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
firstChild returns the first node of any type (element, text, comment). firstElementChild returns only the first Element child, skipping text and comments.
When element children exist, yes — firstElementChild equals children[0]. When there are no element children, firstElementChild is null and children[0] is undefined.
No. It is read-only. To change the first child element, use DOM methods such as prepend(), insertBefore(), or replaceChildren().
It comes from the ParentNode mixin, so Document and DocumentFragment also expose firstElementChild.