lastElementChild property

The lastElementChild property stores the last 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.lastElementChild;

Example

We get a contents of the last child of an element:

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

console.log(text);

The code execution result:

javascript'2'

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

The code execution result:

javascriptnull

See also