I so miss jQuery. I\'m working on a project where I need to get my hands dirty with good \'ol plain Javascript again.
I have this scenario:
parent
The functionality of insertBefore(newNode, referenceNode) is described as:
Inserts the specified node before a reference node as a child of the current node. If
referenceNodeis null, thennewNodeis inserted at the end of the list of child nodes.
And since myNodes[i+1] is outside of the array bounds, it returns undefined, which is treated as null in this case. This means you get your desired behavior.
Edit: Link to the W3 specification of insertBefore()