firstChild property
The firstChild property stores
the element's first child node. If the
element has no children, then
null is returned.
Syntax
element.firstChild;
Example
Let's get a text of the first element node:
<div id="parent">text<p>paragraph</p><!-- comment --></div>
let parent = document.querySelector('#parent');
let text = parent.firstChild.textContent;
console.log(text);
The code execution result:
'text'
See also
-
the
firstElementChildproperty
that contains the first element -
the
lastChildproperty
that contains the last node -
the
childNodesproperty
that contains all element nodes