JavaScript’s Element.lastElementChild read-only property returns the Element’s last child element or null if the Element has no child elements.


An Element object or null if the element has no child elements.


Here is an example of using the lastElementChild property.

Consider the following lines of HTML:

<p id="paragraph">
 <span>I like learning <b>JavaScript</b> programming.</span>

If we run the following lines of JavaScript, what do you think will be logged to the console?

let paragraph = document.getElementById('paragraph');

The correct answer is “SPAN.” That is because the span element is the last – and first – child element of the p element, and we selected the p element by its id and logged the name of its last child element to the console.

What is the difference between lastElementChild and lastChild?

The Element.lastElementChild property is very similar to the Node.lastChild property. Their only difference is that the former ignores Comment and Text nodes and only returns Element nodes.

Browser compatibility

Internet Explorer
Chrome (Android)
Firefox (Android)
Opera (Android)
Safari (iOS)
Samsung Internet
WebView (Android)
lastElementChild 1 12 3.5 9 10 4 18 4 10.1 3 1.0 37

See also