The sibling question
importance: 5
If elem
– is an arbitrary DOM element node…
- Is it true that
elem.lastChild.nextSibling
is alwaysnull
? - Is it true that
elem.children[0].previousSibling
is alwaysnull
?
- Yes, true. The element
elem.lastChild
is 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. SopreviousSibling
may be a text node.
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 []
).