Document.firstElementChild is a read-only property that returns the document’s first child Element, or null if there are none (MDN). For HTML documents, this is usually the root <html> element.
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax
Use document.firstElementChild. Start with this line.
JavaScript
document.firstElementChild
Call
What it does
document.firstElementChild
Primary signature.
Built into the browser. Read or assign document.firstElementChild — no import required.
Library declarations
JavaScript
document.firstElementChild
Parameters
Parameter
Description
Writable?
Optional. Read-only
Return Value
Return Value
Description
*
Element or null
JavaScript Examples
Example 1 — Root <html>
MDN: on HTML documents, the first element child is usually <html>.
Select a container. Here, a <ul> with three items.
Read firstElementChild. First child that is an element.
Whitespace is skipped. Text nodes between tags do not count.
Same property name on Document and Element — different parent node.
Compatibility
Browser Support
Document.firstElementChild is marked Baseline Widely available on MDN (since April 2018). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Document.firstElementChild
Read-only first child Element of the document — usually the root html element.
WidelyAvailable
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLegacy support varies
Partial
Document.firstElementChildBaseline support
Bottom line: Safe to use in modern browsers. See the MDN Document.firstElementChild reference. Prefer documentElement when you specifically mean the HTML root.
Frequently Asked Questions
The document's first child Element, or null if there are no child elements. For HTML documents this is usually the root <html> element (MDN).
No. MDN marks Document.firstElementChild as Baseline Widely available (since April 2018). It is a standard ParentNode property on Document.
firstChild can return any node type — often the DocumentType (doctype). firstElementChild skips non-element children and returns the first Element only.
On normal HTML pages, yes — both typically refer to the <html> element. Prefer document.documentElement when you specifically mean the document root.
Yes. Element.firstElementChild returns the first element child of that element (for example the first <li> inside a <ul>).
When the document has no child elements. Typical HTML pages always have at least <html>, so you usually get an Element.