The Node.firstChild property returns a node’s first child, or null if there are none. Learn the whitespace #text gotcha, firstChild vs firstElementChild, and five practical examples with try-it labs.
01
Kind
Read-only property
02
Returns
Node or null
03
May be
#text / comment
04
Prefer
firstElementChild
05
Empty
null
06
Status
Baseline widely
Fundamentals
Introduction
firstChild is a shortcut for “give me the first entry in childNodes.” It is handy for tree walks and for clearing children with while (node.firstChild) node.removeChild(node.firstChild).
The surprise for beginners: pretty-printed HTML inserts whitespace text nodes, so firstChild is often #text instead of the first tag you see.
💡
Beginner tip
For UI work, prefer Element.firstElementChild. Use firstChild when you truly need any node type (including text).
Concept
Understanding firstChild
MDN: the read-only firstChild property returns the node’s first child in the tree, or null if the node has no children. For a Document, it is the first node in the list of direct children.
Can be an element, text node, comment, or other node type.
Whitespace between tags creates #text nodes.
Same idea as childNodes[0], or null when length is 0.
Read-only — you do not assign to firstChild.
Foundation
📝 Syntax
JavaScript
const first = node.firstChild;
if (first) {
console.log(first.nodeName);
}
Return value
A Node, or null if there are no children.
Compare
⚖️ firstChild vs firstElementChild
node.firstChild
element.firstElementChild
Interface
Node
Element
Returns
Any first child node (or null)
First element child (or null)
Whitespace
Often #text
Skips text / comments
Best for
Full DOM / text-aware code
Most UI element work
Cheat Sheet
⚡ Quick Reference
Goal
Code
Get first child
node.firstChild
Safe read
node.firstChild?.nodeName
First element only
element.firstElementChild
Same as index 0
node.childNodes[0] (or undefined if empty)
Clear children
while (node.firstChild) node.removeChild(node.firstChild);
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Node.firstChild.
Returns
Node | null
First child
Baseline
widely
Since July 2015
Access
read-only
No assignment
Watch for
#text
Whitespace
Hands-On
Examples Gallery
Examples follow MDN Node.firstChild patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
See how source whitespace changes firstChild.
Example 1 — Whitespace Makes #text
MDN-style pretty markup: newline/indent before the span.
Node.firstChild is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Node.firstChild
Safe for production. Remember whitespace text nodes, and prefer firstElementChild for element-only UI code.
UniversalWidely available
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 & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
firstChildExcellent
Bottom line: Read firstChild for any first node type; use firstElementChild when you only want an element.
Wrap Up
Conclusion
Node.firstChild returns the first child node or null. Watch for whitespace #text nodes in indented HTML, prefer firstElementChild for elements, and use the while (firstChild) pattern to clear containers.
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 a standard DOM property — 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.
For a Document, firstChild is the first node in its direct children list — often the doctype, depending on the document.
Did you know?
MDN notes that whitespace between the closing inner tag and the parent’s end tag also creates a trailing #text node—so both ends of a pretty-printed element often have text children, not only the start.