Favorites

DOM Node.firstChild Property

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Node.firstChild
DOM Node PropertyThis property is supported by all modern browsers.
Updated 28th November 2026

The firstChild property returns the first child node of a node, which may be a text node. lastChild returns the last one.

Description

The read-only firstChild property returns the node's first child, and lastChild its last. Because white space between tags creates text nodes, the result is often a text node rather than the element you expect; use firstElementChild and lastElementChild to skip them.

Because they return any kind of node, they're useful when you specifically need text nodes, such as reading or replacing the text at the start of an element without affecting its child elements. Both return null for an element with no children.

When writing HTML, white space between tags, including line breaks and indentation, becomes text nodes, so even <ul> elements that look as if they only contain <li> elements usually start with a text node.

Syntax

node.firstChild
node.lastChild

Value

The first (or last) child node, or null if there are no children.

Example

<ul id="list">
  <li>First</li>
  <li>Last</li>
</ul>
const list = document.getElementById('list');
console.log(list.firstChild.nodeName);
console.log(list.firstElementChild.textContent);
console.log(list.lastElementChild.textContent);

Browser Support

The following information will show you the current browser support for the Node.firstChild property. Hover over a browser icon to see the version that first introduced support for this property.

This property is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee