Element

Element.prototype.lastElementChild

Returns the element's last child Element, or null if there are no child elements

Syntax

JavaScript
element.lastElementChild

Return Value

The last child Element, or null

Examples

Basic Usage
const list = document.querySelector('ul')!
const last = list.lastElementChild
console.log(last?.textContent)
Practical Example
function removeLast(container: HTMLElement) {
  const last = container.lastElementChild
  if (last) last.remove()
}
Advanced Usage
const nav = document.querySelector('nav')!
const lastLink = nav.lastElementChild as HTMLAnchorElement | null
if (lastLink) lastLink.classList.add('last')

Understanding Element.prototype.lastElementChild

The Element.prototype.lastElementChild method in JavaScript returns the element's last child Element, or null if there are no child elements. It belongs to the Element object and is one of the most widely used methods for working with element values in modern JavaScript and TypeScript applications.

The method signature is element.lastElementChild. When called, it returns the last child element, or null. Understanding when and how to use lastElementChild() helps you write more expressive, readable code.

Common use cases for Element.prototype.lastElementChild include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-firstelementchild, dom-children, dom-parentelement, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Element.prototype.lastElementChild is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Element Methods

Other methods in the Element object

Related Tools

More Element Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.