Home

firstChild

firstChild is a standard property of the DOM Node interface that returns the first child node of a given node. It yields a Node object or null when the node has no children.

Because the DOM represents all node types, firstChild can be an element node, a text node, a

firstChild contrasts with firstElementChild, which returns only the first child that is an element node and

For example, in HTML documents, document.firstChild may be a doctype or a text node, while document.documentElement

The property is read-only in the sense that you retrieve the node; to modify the children, you

comment,
or
other
node
types.
This
means
whitespace
between
elements
can
appear
as
a
text
node
and
therefore
be
returned
as
firstChild.
skips
non-element
nodes
such
as
text
or
comments.
As
a
result,
firstElementChild
is
usually
preferred
when
you
want
the
first
HTML
element.
is
the
HTML
<html>
element.
To
reliably
access
the
first
element,
you
would
typically
use
document.documentElement
or,
for
a
particular
container,
container.firstElementChild.
use
DOM
methods
such
as
insertBefore,
appendChild,
or
removeChild.
firstChild
is
part
of
the
core
DOM
and
is
widely
supported
across
major
browsers.
It
can
be
null
if
there
are
no
children,
and
its
value
reflects
the
current
document
structure
as
nodes
are
added
or
removed.