Element.before() is an instance method from the ChildNode API. It inserts nodes or strings in the parent’s child list, just before the element you call it on. Learn multi-argument inserts, text coercion, how it compares to after(), and five try-it labs.
01
Kind
Instance method
02
Inserts
Before this element
03
Args
Nodes or strings
04
Returns
undefined
05
Throws
HierarchyRequestError
06
Status
Baseline widely
Fundamentals
Introduction
Older DOM code often used parent.insertBefore(newNode, ref) to place something before a sibling. before() makes that intent clear: call it on the element that should come second, and pass what should appear ahead of it.
You can pass elements, other nodes, or plain strings. Strings become Text nodes automatically—handy for labels and spacing without creating nodes by hand.
💡
Beginner tip
before() inserts into the parent of the element you call it on. The element itself is the landmark; new content becomes its previous sibling(s). Compare with after() for inserting after the element.
param1, …, paramN — a set of Node objects or strings to insert. Strings are inserted as equivalent Text nodes.
Return value
None (undefined).
Exceptions
HierarchyRequestErrorDOMException — thrown when the node cannot be inserted at the specified point in the hierarchy (MDN).
Common patterns
JavaScript
const p = document.querySelector("p");
const span = document.createElement("span");
p.before(span); // insert one element
p.before("Text"); // insert a Text node
p.before(span, "Text"); // element, then text
p.before(" · ", span, " · "); // several siblings in order
Cheat Sheet
⚡ Quick Reference
Goal
Code
Insert element before el
el.before(newEl)
Insert text before el
el.before("Hello")
Insert several items
el.before(a, b, "c")
Classic equivalent
parent.insertBefore(node, el)
Return value
undefined
MDN status
Baseline Widely available (since April 2018)
Snapshot
🔍 At a Glance
Four facts to remember about Element.before().
Returns
undefined
Updates the DOM in place
Baseline
widely
Since April 2018
Where
parent list
Previous sibling(s) of el
Strings
→ Text
Auto Text nodes
Compare
📋 before() vs after() vs insertBefore()
el.before(...)
el.after(...)
parent.insertBefore(n, ref)
Called on
The sibling landmark
The sibling landmark
The parent
Position
Immediately before el
Immediately after el
Before ref (or end if null)
Strings
Yes (as Text)
Yes (as Text)
No — need a Node
Return value
undefined
undefined
Inserted node
Best for
Clear “insert before me”
Clear “insert after me”
Precise parent + reference
Hands-On
Examples Gallery
Examples follow MDN Element.before() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
Insert an element, text, or both—matching MDN’s core demos.
Example 1 — Inserting an Element (MDN)
Place a new <span> immediately before a <p>.
JavaScript
let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
let span = document.createElement("span");
p.before(span);
console.log(container.outerHTML);
// "<div><span></span><p></p></div>"
span becomes the sibling immediately before p under container. You never call a method on the parent for this case.
Example 2 — Inserting Text (MDN)
Pass a string; the browser creates a Text node for you.
JavaScript
let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
p.before("Text");
console.log(container.outerHTML);
// "<div>Text<p></p></div>"
No document.createTextNode needed. The string is inserted as a Text node before p.
📈 Practical Patterns
Combine args, insert several siblings, and move existing nodes.
Example 3 — Element and Text Together (MDN)
Pass an element and a string in one call—order is preserved.
JavaScript
let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
let span = document.createElement("span");
p.before(span, "Text");
console.log(container.outerHTML);
// "<div><span></span>Text<p></p></div>"
One call inserts three nodes before item: a strong badge, a separator Text node, and an emphasis element—in that order.
Example 5 — Moving an Existing Node
If the node is already in the document, before() relocates it.
JavaScript
const list = document.getElementById("list");
const a = document.getElementById("a");
const b = document.getElementById("b");
b.before(a);
console.log([...list.children].map((el) => el.id).join(", "));
// "a, b" (a moved to sit before b)
Element.before() is Baseline Widely available (MDN: across browsers since April 2018). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.before()
Safe for production. Insert nodes or text before an element with clear, modern DOM APIs.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerNot supported · Use insertBefore polyfill pattern
No
before()Excellent
Bottom line: Prefer before() when you think “put this before that element.” Use insertBefore when you already work from the parent and a reference child. Strings become Text nodes automatically.
Wrap Up
Conclusion
Element.before() is the modern way to insert siblings before an element—nodes, text, or several at once. It returns undefined and updates the parent’s child list in place.
Confuse sibling insert with appending inside the element
Assume IE supports before() without a fallback
Forget that invalid trees can throw HierarchyRequestError
Clone by accident—moves remove the old location
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.before()
Insert siblings before an element—nodes or text, one call.
5
Core concepts
📝01
Returns
undefined
API
📄02
Where
before this el
Sibling
✍️03
Strings
become Text
Coerce
✅04
Baseline
widely available
Status
⚡05
Existing
moves
DOM
❓ Frequently Asked Questions
It inserts one or more Node objects or strings into the children of this element’s parent, immediately before this element. Strings become Text nodes.
No. MDN marks Element.before() as Baseline Widely available (across browsers since April 2018). It is not Deprecated, Experimental, or Non-standard.
Nothing useful — the return value is undefined. The DOM is updated in place; check parent.outerHTML or previousSibling if you need to verify the result.
Yes. before(node1, node2, …) inserts all arguments in order, right before the element you call it on.
before() inserts siblings immediately before the element you call it on. after() inserts immediately after. Both are ChildNode helpers; insertBefore() is the classic parent-based API.
If the element is not in a parent’s child list, before() has nowhere to insert. Invalid hierarchy cases can throw a HierarchyRequestError DOMException.
Did you know?
before() comes from the ChildNode mixin—the same family as after(), remove(), and replaceWith(). That is why you call it on the sibling you care about, not on the parent container.