JavaScript lastElementChild Property Tutorial

In this section, we will learn what the Element lastElementChild property is and how to use it in JavaScript.

What is Element lastElementChild Property in JavaScript?

The JavaScript Element lastElementChild property is used to get the last child of an element that is an Element-Node and return a reference of that child-element as a result.

Note: the lastChildElement will return the last child of an element that is an Element-Node! That means for the lastChildElement property, if the last child of an element is in fact of type Text-Node for example, that will be ignored and instead this property will look for the last occurrence of a child that is an Element-Node. But there’s another property called lastChild and using this property will return the last child no-matter if it’s an Element-Node, Text-Node or a Comment-Node. So if you need to get the last child of an element and you don’t care if that element might be a Text-Node or a Comment-Node, then use the lastChild property instead of the lastElementChild.

JavaScript lastElementChild Property Syntax:

element.lastElementChild;

JavaScript lastElementChild Property Input Value

The lastElementChild property is read-only and it doesn’t take any input.

JavaScript lastElementChild Property Return Value

The return value of this method is a reference to the last child of the target element, and this child is of type Element-Node.

Note: if the target element does not have a child, the return value of this method becomes null.

Example: using lastElementChild Property in JavaScript

See the Pen using lastElementChild Property in JavaScript by Omid Dehghan (@odchan1) on CodePen.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies