lastChild property
The lastChild property stores
the last element node. If the element
has no children, then null is
returned.
Syntax
element.lastChild;
Example
Let's obtain a text of the last element node:
<div id="parent">text<p>paragraph</p><!-- comment --></div>
let parent = document.querySelector('#parent');
let text = parent.lastChild.textContent;
console.log(text);
The code execution result:
' comment '
See also
-
the
lastElementChildproperty
that contains the last element -
the
firstChildproperty
that contains the first node -
the
childNodesproperty
that contains all element nodes