The sibling question
importance: 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, so if there are children, then yes. - No, wrong, because
elem.children[0]is the first child among elements. But there may be non-element nodes before it. SopreviousSiblingmay be a text node.
Please note that for both cases if there are no children, then there will be an error. For instance, if elem.lastChild is null, we can’t access elem.lastChild.nextSibling.