Example 1 — Read the Last Child Element
Log the text of the last <li> in a list (MDN shape).
const list = document.getElementById("list");
console.log(list.lastElementChild.textContent);
// "Third (3)" How It Works
- Find the parent.
getElementById("list")returns the<ul>. - Read
lastElementChild. It is the last<li>. - Read text.
textContentis"Third (3)". - No null here. The list has element children, so the property is not
null.

