The Node.replaceChild() method swaps one child for another on a parent. Learn the unusual new before old argument order, what happens when the new node is already in the tree, how the return value is the old child, and how this pairs with removeChild() and appendChild().
01
Kind
Instance method
02
Args
new, then old
03
Returns
oldChild
04
Moves
Existing newChild
05
Alt
replaceWith()
06
Status
Baseline widely
Fundamentals
Introduction
Sometimes you do not only remove a child—you put something else in the same spot. parent.replaceChild(newChild, oldChild) does that in one step: the old child leaves, the new child takes its place among the siblings.
MDN highlights two beginner traps: the parameter order is new first, old second, and if newChild already lives elsewhere in the document, the browser moves it (it is removed from the old location first).
💡
Beginner tip
For elements only, oldChild.replaceWith(newChild) often reads more clearly. Keep replaceChild when you already hold the parent or follow classic Node tutorials.
Concept
Understanding replaceChild()
Call it on the parent. Pass the node that should appear, then the child that should leave. Get the old child back as the return value.
Swap — newChild takes oldChild’s place.
Order — (newChild, oldChild)—new before old.
Return — the replaced node (same as oldChild).
Move — an already-attached newChild is relocated, not cloned.
Node.replaceChild() 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.replaceChild()
Safe, long-standing API for swapping a child in place on its parent.
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
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
replaceChild()Excellent
Bottom line: Replaces oldChild with newChild; returns oldChild; moves newChild if already in the DOM.
Wrap Up
Conclusion
Node.replaceChild() swaps children on a parent: new node in, old node out (and returned). Memorize (newChild, oldChild), remember that existing nodes move, and consider replaceWith when you already hold the old element.
Expect moves when reusing a live DOM node as newChild
Prefer replaceWith for simple element swaps
❌ Don’t
Reverse the argument order by habit
Assume newChild is cloned—it is moved
Replace with a node that would create a DOM cycle
Confuse DOM Node with the Node.js runtime
Forget NotFoundError when parents do not match
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about replaceChild()
Swap in place—new first, old second.
5
Core concepts
🔄01
Swaps child
same parent
API
📝02
new, old
order matters
Args
📤03
Returns old
reusable
Return
🚀04
Moves live
newChild
DOM
⚠️05
Watch errors
NotFound / Hierarchy
Safety
❓ Frequently Asked Questions
It replaces one child of a parent with another node. Call it on the parent: replaceChild(newChild, oldChild). It returns the replaced node (the same as oldChild).
No. MDN marks Node.replaceChild() as Baseline Widely available (since July 2015). It is a standard DOM method — not Deprecated, Experimental, or Non-standard.
MDN notes the order is unusual: newChild first, oldChild second. Remember “put the new one in, take the old one out.” Element.replaceWith() can be easier to read for elements.
MDN warns that if the new node is already present somewhere else in the DOM, it is first removed from that position, then inserted in place of oldChild.
NotFoundError if oldChild is not a child of the parent you called. HierarchyRequestError when the replacement would violate DOM tree rules (cycles, wrong node types, invalid document structure).
If you already have the old element and only need to swap it, oldChild.replaceWith(newChild) reads more naturally. Use replaceChild when you work from the parent or need classic Node API patterns.
Did you know?
MDN calls out the argument order as unusual on purpose—many developers instinctively write “old, then new.” Saying “replace with X, remove Y” (new, old) once while coding saves a surprising number of runtime mistakes.