parent.insertBefore(newNode, referenceNode) inserts newNode as a child of parent, immediately beforereferenceNode(MDN). If referenceNode is null, the node is added at the end.
Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. There is no insertAfter() — emulate with nextSibling.
Remember
parent.insertBefore(newNode, referenceNode)
→ newNode sits just before referenceNode
→ returns newNode (empty fragment if DocumentFragment)
referenceNode === null → insert at end (like appendChild)
Existing newNode in document → moved (not copied)
insertAfter?
parent.insertBefore(newNode, ref.nextSibling)
Second argument is required. Pass null explicitly (MDN).
Null means “end”. Node becomes the last child.
Same idea as appendChild. Useful when the reference may be missing.
Do not omit the arg. Invalid / missing values differ by browser.
Example 3 — Emulate insertAfter
There is no insertAfter(). Use nextSibling (MDN).
JavaScript
const parent = document.getElementById("parent");
const sp2 = document.getElementById("child");
const sp1 = document.createElement("span");
sp1.textContent = "AFTER";
parent.insertBefore(sp1, sp2.nextSibling);
// if sp2 is last, nextSibling is null → sp1 goes at the end
Want after sp2. Insert before whatever follows sp2.
Use nextSibling. That node becomes the reference.
Last child case.nextSibling is null → appends at end.
Same method. Still insertBefore — no separate API.
Edge Cases & Pitfalls
Check these before relying on insertBefore() in real UIs.
args
Second argument is required
MDN: always pass a Node or null. Omitting it or passing invalid values can differ by browser.
move
Existing nodes are moved
A node cannot live in two places. Use cloneNode() when you need a copy.
ref
Reference must be a child of the parent
Inserting with a reference that is not under that parent can throw (hierarchy / NotFound errors).
modern
Element.before / prepend alternatives
For element siblings, before() / prepend() can be clearer — insertBefore still works on any Node parent.
Compatibility
Browser Support
Node.insertBefore() is marked Baseline Widely available on MDN (since July 2015).
✓ Baseline · Widely available
Node.insertBefore()
Inserts a child before a reference node (or at the end when the reference is null). Moves existing nodes. Returns the inserted child.
100%Widely available
Google ChromeYes (Baseline)
Yes
Microsoft EdgeYes (Baseline)
Yes
Mozilla FirefoxYes (Baseline)
Yes
Apple SafariYes (Baseline)
Yes
OperaYes (Baseline)
Yes
Internet ExplorerSupported (legacy)
Yes
insertBefore()Excellent
Bottom line: Safe everyday DOM API. Always pass the second argument. See the MDN insertBefore reference.
Analysis
Performance Notes
Approach
Cost
Best for
One insertBefore
One DOM update
Single ordered insert
Many inserts in a loop
Can reflow often
Avoid for large lists
DocumentFragment then one insert
One attach
Batch many children
For many nodes, build into a fragment, then insertBefore once — fewer layout thrashers than N separate inserts.
Remember
Key Takeaways
Inserts before:newNode sits just ahead of referenceNode.
null = end: same idea as appending.
Moves: existing document nodes relocate — clone to copy.
No insertAfter: use ref.nextSibling as the reference.
One line:insertBefore(newNode, ref) places a child before a sibling — pass null for the end, or ref.nextSibling for after.
Frequently Asked Questions
It inserts newNode as a child of the parent, immediately before referenceNode. If referenceNode is null, newNode is inserted at the end of the parent’s children.
No. MDN marks Node.insertBefore() as Baseline Widely available (since July 2015). It is a standard DOM method — not Deprecated, Experimental, or Non-standard.
insertBefore moves it from its old position to the new one. A node cannot be in two places at once. Use cloneNode() if you need a copy.
No. MDN shows you can emulate it with parent.insertBefore(newNode, referenceNode.nextSibling). If there is no next sibling, nextSibling is null and the node is appended at the end.
No. MDN says referenceNode is required — pass a Node or explicitly null. Omitting it or passing invalid values can behave differently across browsers.
It returns the inserted child. If newNode is a DocumentFragment, it returns the emptied fragment after its contents move.