node.compareDocumentPosition(otherNode) reports where otherNode sits relative to node(MDN) — as a bitmask of DOCUMENT_POSITION_* flags (or 0 if they are the same node).
Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. Always test flags with bitwise & (MDN).
Remember
node.compareDocumentPosition(other)
→ bitmask (or 0 if same node)
1 DISCONNECTED
2 PRECEDING (other before node)
4 FOLLOWING (other after node)
8 CONTAINS (other is ancestor)
16 CONTAINED_BY (other is descendant)
32 IMPLEMENTATION_SPECIFIC
Test: result & Node.DOCUMENT_POSITION_FOLLOWING
Call it on one node, pass the other, then test bits with &.
Pick the reference node (node).
Call node.compareDocumentPosition(other).
Test flags: result & Node.DOCUMENT_POSITION_FOLLOWING, etc.
Remember several flags can be set together.
Piece
Required?
Role
otherNode
Yes
Node to locate relative to this
Return
—
Integer bitmask (or 0)
Test
Yes
Bitwise & with a flag constant
Pseudocode
Pseudocode
mask = a.compareDocumentPosition(b)
if mask == 0:
a and b are the same node
else if mask & FOLLOWING:
b comes after a in document order
else if mask & PRECEDING:
b comes before a
if mask & CONTAINED_BY:
b is inside a (descendant)
if mask & CONTAINS:
b is an ancestor of a
Reference
Cheat Sheet
Keep these flags handy when reading the bitmask.
Flag
Value
Meaning (other vs this)
DISCONNECTED
1
Different trees / documents
PRECEDING
2
other before this
FOLLOWING
4
other after this
CONTAINS
8
other is ancestor
CONTAINED_BY
16
other is descendant
IMPLEMENTATION_SPECIFIC
32
Order not portable
Same node
0
No bits set
Try it
Live Preview
Compare #a to another node. Flags are tested with bitwise &.
Tree
A (reference)
B (inside wrap)
C (after A)
Browser demo of Node.compareDocumentPosition().
Result
Pick a pair, then Compare…
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Does body Follow head?
MDN-style bitmask check with &.
JavaScript
const head = document.head;
const body = document.body;
const mask = head.compareDocumentPosition(body);
if (mask & Node.DOCUMENT_POSITION_FOLLOWING) {
console.log("Well-formed document");
} else {
console.error(" is not before ");
}
Fine for UI logic. Prefer the smallest API that answers your question — do not decode a bitmask when contains() is enough.
Remember
Key Takeaways
Bitmask: relative position of other to this.
Test with &: never assume a single exact number.
0: same node.
CONTAINS vs CONTAINED_BY: ancestor vs descendant.
One line:compareDocumentPosition answers “where is the other node?” via combinable position flags.
Frequently Asked Questions
It reports where otherNode sits relative to the node you call it on, as a bitmask of DOCUMENT_POSITION_* flags (or 0 if both are the same node).
No. MDN marks Node.compareDocumentPosition() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Because the return value is a bitmask. Several flags can be set at once. Use result & Node.DOCUMENT_POSITION_FOLLOWING (and similar) to test each flag.
If otherNode is the same node as this node, compareDocumentPosition returns 0 (no position bits).
CONTAINS (8) means otherNode is an ancestor of this node. CONTAINED_BY (16) means otherNode is a descendant of this node.
Yes. For example, if otherNode precedes this node and also contains it, both PRECEDING and CONTAINS are set (value 10).