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

Value

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

Examples

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>
</p>

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

let paragraph = document.getElementById('paragraph');
console.log(paragraph.lastElementChild.nodeName);

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

Chrome
Edge
Firefox
Internet Explorer
Opera
Safari
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