The Node.insertBefore() method inserts a node before a reference child. Learn null as “append at end,” how existing nodes move, and how to emulate insertAfter with nextSibling.
01
Kind
Instance method
02
Inserts
Before reference
03
null ref
Appends at end
04
Existing
Moves (no clone)
05
insertAfter
Use nextSibling
06
Status
Baseline widely
Fundamentals
Introduction
appendChild() always adds at the end. insertBefore() lets you pick the spot: put the new child immediately before another child you already have.
Like appendChild, inserting a node that already lives in the document moves it. Pass null as the reference to append at the end—and always pass the second argument explicitly.
💡
Beginner tip
There is no insertAfter(). Use parent.insertBefore(newNode, ref.nextSibling).
Concept
Understanding insertBefore()
MDN: inserts a node before a reference node as a child of a specified parent node.
Parent — the node you call the method on.
newNode — the node to insert (or move).
referenceNode — existing child; insert immediately before it.
null reference — insert as the last child (same idea as append).
Foundation
📝 Syntax
JavaScript
parent.insertBefore(newNode, referenceNode);
Parameters
newNode — The node to insert.
referenceNode — The child before which to insert. Must be provided: a Node or null (append at end).
Return value
The added child. For a DocumentFragment, the emptied fragment is returned after its contents move.
Compare
⚖️ insertBefore() vs appendChild()
insertBefore()
appendChild()
Position
Before a reference (or end if null)
Always last child
Args
newNode, referenceNode
child only
Existing node
Moves
Moves
Equivalent append
insertBefore(node, null)
appendChild(node)
Best for
Precise sibling order
Simple “add at end”
Cheat Sheet
⚡ Quick Reference
Goal
Code
Insert before a sibling
parent.insertBefore(newNode, ref)
Append (via insertBefore)
parent.insertBefore(newNode, null)
Insert after a sibling
parent.insertBefore(newNode, ref.nextSibling)
Insert as first child
parent.insertBefore(newNode, parent.firstChild)
Copy instead of move
parent.insertBefore(node.cloneNode(true), ref)
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Node.insertBefore().
Returns
Node (child)
Inserted node
Baseline
widely
Since July 2015
null ref
append end
Like appendChild
Existing
moves
Does not clone
Hands-On
Examples Gallery
Examples follow MDN Node.insertBefore() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
Insert before a sibling, or append with an explicit null.
Example 1 — Insert Before an Existing Child (MDN)
Create a node and place it immediately before a known sibling.
Always pass the second argument. Use null on purpose when you want append-style behavior.
📈 Move, insertAfter & First Child
Relocate nodes and place them after or at the start.
Example 3 — Inserting Moves an Existing Node
You do not need to remove the node first.
JavaScript
const list = document.getElementById("list");
const first = document.getElementById("a");
const second = document.getElementById("b");
list.insertBefore(second, first);
console.log(list.children[0].id); // "b"
console.log(list.children[1].id); // "a"
Node.insertBefore() is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Node.insertBefore()
Safe for production. Pass null to append; use nextSibling to insert after.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Node.insertBefore() places a child before a reference node (or at the end when the reference is null). Existing nodes move; emulate insert-after with nextSibling.
Pass null explicitly when you want append behavior
Use ref.nextSibling to insert after
Expect moves when inserting an existing node
Use firstChild / firstElementChild to prepend carefully
Clone when you need a duplicate, not a move
❌ Don’t
Omit the second argument
Assume an insertAfter API exists
Forget whitespace when using firstChild vs firstElementChild
Confuse DOM Node with the Node.js runtime
Pass an invalid reference type and expect portable behavior
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about insertBefore()
Insert a child before a reference—or at the end with null.
5
Core concepts
⬅️01
Before ref
precise position
API
∅02
null = end
like append
Arg
🔁03
Moves
existing nodes
Gotcha
➜04
insertAfter
nextSibling
Pattern
📈05
firstChild
prepend
Start
❓ 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.
Did you know?
Modern Element APIs like before(), after(), and prepend() are often clearer for elements—but insertBefore remains the classic Node method that works on any Node parent and is everywhere in older code.