The read-only firstChild property returns the node’s first child in the tree, or null if there are none (MDN). That child may be an element, text, or comment — not only a tag.
Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. Whitespace often makes firstChild a #text node — prefer firstElementChild for the first element.
Remember
parent.firstChild
→ first Node in childNodes (or null)
Same idea: parent.childNodes[0]
May be:
Element → "SPAN", "DIV", …
Text → "#text" (common with indented HTML)
Comment → "#comment"
Need first element only?
parent.firstElementChild
Read it, null-check, then inspect nodeName / nodeType.
Get a parent node.
Read parent.firstChild.
If not null, use the node (or compare with firstElementChild).
For UI that needs a tag, prefer firstElementChild.
Piece
Required?
Role
parent
Yes
Node whose first child you want
Return
—
Node or null
Writable?
No
Read-only
Pseudocode
Pseudocode
child = parent.firstChild
if child is null:
parent has no children
else:
// may be #text because of whitespace
name = child.nodeName
want a tag?
el = parent.firstElementChild
Reference
Cheat Sheet
Keep these facts handy when reading the first child.
Topic
Detail
Returns
Node or null
Equals
childNodes[0] when children exist
Whitespace
Often yields #text
Elements only
firstElementChild
Empty parent
null — check first
Standard
Baseline Widely available (since July 2015)
Try it
Live Preview
Compare indented vs compact markup for the same visual content.
Samples
Pretty span
Compact span
Browser demo of Node.firstChild vs firstElementChild.
Result
Pick indented or compact, then Inspect…
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
When children exist, firstChild === childNodes[0].
Compatibility
Browser Support
Node.firstChild is marked Baseline Widely available on MDN (since July 2015).
✓ Baseline · Widely available
Node.firstChild
Returns the first child Node or null. May be a text node from whitespace. Prefer firstElementChild for the first element.
100%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
firstChildExcellent
Bottom line: Safe everyday DOM API. Null-check, expect #text from pretty HTML, and use firstElementChild for tags. See the MDN firstChild reference.
Analysis
Performance Notes
Approach
Cost
Best for
firstChild
O(1)
Any first node type
firstElementChild
O(1)
First element tag
childNodes[0]
O(1)
Same as firstChild
Reading either property is cheap. Choose by meaning (any node vs element), not by speed.
Remember
Key Takeaways
First child node — or null if empty.
Whitespace: often #text before your first tag.
UI tags: use firstElementChild.
Null-check before using the result.
One line:firstChild is the first node in the child list — frequently whitespace text in pretty HTML.
Frequently Asked Questions
The node's first child in the tree, or null if there are no children. The child can be an Element, Text, Comment, or another node type — not only an HTML element.
No. MDN marks Node.firstChild as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Whitespace between tags in the HTML source (spaces, newlines, tabs) becomes a Text node. So firstChild is often #text even when the first visible tag is a span or div.
Use Element.firstElementChild when you want the first element child and want to skip text and comment nodes. Prefer it for most UI work.
firstChild is null. Always check before reading nodeName or other properties on the result.
firstChild is the same as childNodes[0] when children exist (or null when the list is empty).