firstElementChild property

The firstElementChild property contains the first child element. All tags that are directly located inside a block are considered child elements. If the element has no children, then null is returned.

Syntax

javascriptelement.firstElementChild;

Example

Let's get a content of the element's first child:

html<div id="parent">
  <p>1</p>
  <p>2</p>
</div>
javascriptlet parent = document.querySelector('#parent');
let text = parent.firstElementChild.textContent;

console.log(text);

The code execution result:

javascript'1'

Example

And now an element has no children and therefore null will be displayed:

html<div id="parent"></div>
javascriptlet parent = document.querySelector('#parent');
console.log(parent.firstElementChild);

The code execution result:

javascriptnull

See also