offline version v3


157 of 264 menu

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

element.lastElementChild;

Example

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

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

The code execution result:

'2'

Example

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

<div id="parent"></div> let parent = document.querySelector('#parent'); console.log(parent.lastElementChild);

The code execution result:

null

See also

enru