The sibling question
önem: 5
If elem – is an arbitrary DOM element node…
- Is it true that
elem.lastChild.nextSiblingis alwaysnull? - Is it true that
elem.children[0].previousSiblingis alwaysnull?
- Yes, true. The element
elem.lastChildis always the last one, it has nonextSibling. - No, wrong, because
elem.children[0]is the first child among elements. But there may exist non-element nodes before it. SopreviousSiblingmay be a text node. Also, if there are no children, then trying to accesselem.children[0]
Please note: for both cases if there are no children, then there will be an error.
If there are no children, elem.lastChild is null, so we can’t access elem.lastChild.nextSibling. And the collection elem.children is empty (like an empty array []).