JavaScript Node firstChild Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Read-only

What You’ll Learn

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

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).

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.

📝 Syntax

JavaScript
const first = node.firstChild;
if (first) {
  console.log(first.nodeName);
}

Return value

A Node, or null if there are no children.

⚖️ firstChild vs firstElementChild

node.firstChildelement.firstElementChild
InterfaceNodeElement
ReturnsAny first child node (or null)First element child (or null)
WhitespaceOften #textSkips text / comments
Best forFull DOM / text-aware codeMost UI element work

⚡ Quick Reference

GoalCode
Get first childnode.firstChild
Safe readnode.firstChild?.nodeName
First element onlyelement.firstElementChild
Same as index 0node.childNodes[0] (or undefined if empty)
Clear childrenwhile (node.firstChild) node.removeChild(node.firstChild);
MDN statusBaseline Widely available (since July 2015)

🔍 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

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.

JavaScript
// <p id="para-01">
//   <span>First span</span>
// </p>

const p01 = document.getElementById("para-01");
console.log(p01.firstChild.nodeName); // "#text"
Try It Yourself

How It Works

Any whitespace between the opening <p> and <span> becomes a text node, so that text node is first.

Example 2 — Compact Markup → Element

Remove the whitespace and the span becomes the first child.

JavaScript
// <p id="para-01"><span>First span</span></p>

const p01 = document.getElementById("para-01");
console.log(p01.firstChild.nodeName); // "SPAN"
Try It Yourself

How It Works

With no text between tags, the first child is the element node you intended.

📈 firstElementChild, null & Clearing

Skip text nodes, handle empty parents, drain children safely.

Example 3 — firstChild vs firstElementChild

Skip whitespace by asking for the first element child.

JavaScript
const p = document.getElementById("para-01");
console.log(p.firstChild.nodeName);          // often "#text"
console.log(p.firstElementChild.nodeName);   // "SPAN"
Try It Yourself

How It Works

firstElementChild ignores text and comment nodes and returns the first element, or null if none exist.

Example 4 — Empty Node Returns null

Always guard before reading properties on firstChild.

JavaScript
const empty = document.createElement("div");
console.log(empty.firstChild); // null

if (empty.firstChild) {
  console.log(empty.firstChild.nodeName);
} else {
  console.log("No children");
}
Try It Yourself

How It Works

Reading .nodeName on null throws. Check the result (or use optional chaining) first.

Example 5 — Clear Children with firstChild

Classic live-safe drain loop used with childNodes too.

JavaScript
const box = document.getElementById("box");

while (box.firstChild) {
  box.removeChild(box.firstChild);
}

console.log(box.firstChild); // null
Try It Yourself

How It Works

Each removal advances what counts as firstChild until none remain and the property becomes null.

🚀 Common Use Cases

  • Empty a containerwhile (el.firstChild) el.removeChild(el.firstChild).
  • Inspect leading text — read a leading text node before the first tag.
  • Teach the DOM — show why indented HTML creates #text.
  • Prefer elements for UI — switch to firstElementChild for tags.
  • Document inspection — look at document.firstChild (often the doctype).

🧠 How firstChild Works

1

Look at childNodes

The engine considers the parent’s direct children in order.

Tree
2

Pick index 0

Return that node, whatever its type (element, text, …).

First
3

Or return null

If there are no children, firstChild is null.

Empty
4

Your script branches

Check for null, then read nodeName / nodeType.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Whitespace and comments can be the first child.
  • Use firstElementChild when you only want elements.
  • Related: childNodes, baseURI, JavaScript hub.

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Long-standing support in legacy IE
Full support
firstChild Excellent

Bottom line: Read firstChild for any first node type; use firstElementChild when you only want an element.

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.

Continue with isConnected, childNodes, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Null-check firstChild before using it
  • Use firstElementChild for element-only needs
  • Expect #text in pretty-printed HTML
  • Clear nodes with while (node.firstChild)
  • Compare with childNodes[0] when debugging

❌ Don’t

  • Assume firstChild is always an element
  • Read .nodeName on a possible null
  • Assign to firstChild (read-only)
  • Ignore comments that might lead the child list
  • Confuse DOM Node with the Node.js runtime

Key Takeaways

Knowledge Unlocked

Five things to remember about firstChild

First child node—or null.

5
Core concepts
02

#text trap

whitespace

Gotcha
⚖️ 03

vs element

firstElementChild

Compare
🔒 04

Null-safe

check first

Safety
🗑️ 05

Clear loop

while firstChild

Pattern

❓ 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 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.

Next: isConnected

Check whether a node is still connected to a Document.

isConnected →

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