Element.prototype.firstElementChild
Returns the element's first child Element, or null if there are no child elements
Syntax
element.firstElementChildReturn Value
The first child Element, or null
Examples
const list = document.querySelector('ul')!
const first = list.firstElementChild
console.log(first?.textContent)function highlightFirst(container: HTMLElement) {
const first = container.firstElementChild as HTMLElement | null
if (first) first.style.fontWeight = 'bold'
}const table = document.querySelector('table')!
const firstRow = table.tBodies[0].firstElementChild
console.log(firstRow?.innerHTML)Understanding Element.prototype.firstElementChild
The Element.prototype.firstElementChild method in JavaScript returns the element's first 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.firstElementChild. When called, it returns the first child element, or null. Understanding when and how to use firstElementChild() helps you write more expressive, readable code.
Common use cases for Element.prototype.firstElementChild include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-lastelementchild, dom-children, dom-parentelement, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Element.prototype.firstElementChild 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
Element.prototype.lastElementChildReturns the element's last child Element, or null if there are no child elements
Element.prototype.childrenReturns a live HTMLCollection of the child elements of the element
Element.prototype.parentElementReturns the parent Element of the specified element, or null if the element has no parent or the parent is not an Element
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.