JavaScript Node firstChild Property

Beginner
5 min read
Updated: Oct 2026
5 examples
Live preview

What Is firstChild?

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
GoalPrefer
Any first child nodefirstChild
First element childfirstElementChild
Last child nodelastChild

How to Use firstChild

Read it, null-check, then inspect nodeName / nodeType.

  1. Get a parent node.
  2. Read parent.firstChild.
  3. If not null, use the node (or compare with firstElementChild).
  4. For UI that needs a tag, prefer firstElementChild.
PieceRequired?Role
parentYesNode whose first child you want
Return—Node or null
Writable?NoRead-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

Cheat Sheet

Keep these facts handy when reading the first child.

TopicDetail
ReturnsNode or null
EqualschildNodes[0] when children exist
WhitespaceOften yields #text
Elements onlyfirstElementChild
Empty parentnull — check first
StandardBaseline Widely available (since July 2015)

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…

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Read firstChild

Log the first child’s nodeName when it exists.

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

if (child) {
  console.log(child.nodeName);
} else {
  console.log("no children");
}
Try It Yourself

How It Works

  1. Select the parent. Any node that may have children.
  2. Read firstChild. First entry of the child list.
  3. Null-check. Empty parents return null.
  4. Log nodeName. Shows the kind of node you got.

Example 2 — Whitespace → #text

MDN demo: indented markup inserts a text node first.

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

How It Works

  1. Pretty HTML has newlines. Between <p> and <span>.
  2. Browser adds a Text node. That becomes firstChild.
  3. nodeName is #text. Not the span you see on screen.
  4. Compact HTML removes that text node.

Example 3 — Prefer firstElementChild

Skip text/comments when you want the first tag (MDN tip).

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

How It Works

  1. Read firstChild. May be whitespace text.
  2. Read firstElementChild. Skips non-elements.
  3. Compare names. #text vs SPAN.
  4. Use firstElementChild for everyday UI code.

Example 4 — Empty Parent → null

Always guard before touching properties on the result.

JavaScript
const empty = document.getElementById("empty");
console.log(empty.firstChild); // null
console.log(empty.hasChildNodes()); // false
Try It Yourself

How It Works

  1. Empty element. No child nodes at all.
  2. firstChild is null. Not an empty string.
  3. hasChildNodes is false. Same idea as a boolean check.
  4. Avoid .nodeName on null — that throws.

Example 5 — Remove the First Child

Classic clear step: remove firstChild while it exists.

JavaScript
const box = document.getElementById("box");
if (box.firstChild) {
  box.removeChild(box.firstChild);
}
console.log(box.firstChild && box.firstChild.nodeName);
Try It Yourself

How It Works

  1. Check firstChild. Skip if already empty.
  2. removeChild that node. Drops the current first child.
  3. List shifts. A former sibling becomes firstChild.
  4. Loop while firstChild to clear everything (see childNodes).

Edge Cases & Pitfalls

Check these before treating firstChild as “the first tag.”

whitespace

Indented HTML → #text

MDN: spaces/newlines between tags create a text node that becomes firstChild.

null

No children → null

Always null-check before reading nodeName or calling methods on the result.

elements

Need a tag → firstElementChild

Use firstElementChild to skip text and comments.

alias

Same as childNodes[0]

When children exist, firstChild === childNodes[0].

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
firstChild Excellent

Bottom line: Safe everyday DOM API. Null-check, expect #text from pretty HTML, and use firstElementChild for tags. See the MDN firstChild reference.

Performance Notes

ApproachCostBest for
firstChildO(1)Any first node type
firstElementChildO(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.

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

Next: isConnected

Learn whether a node is connected to the document.

isConnected tutorial →

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.

8 people found this page helpful