The Node.compareDocumentPosition() method reports where another node sits relative to this one. Learn the DOCUMENT_POSITION_* bitmask flags, how to test them with &, and common ancestor / order checks.
01
Kind
Instance method
02
Returns
Bitmask integer
03
Test with
Bitwise AND (&)
04
Same node
Returns 0
05
Flags
Precede / contain…
06
Status
Baseline widely
Fundamentals
Introduction
Sometimes you need more than “is this inside that?” You may need document order: does A come before B? Is B an ancestor? compareDocumentPosition() answers with a single integer bitmask.
MDN stresses that because the result is a bitmask, you must use the bitwise AND operator (&) for meaningful checks—not === against one flag alone when multiple bits might be set.
💡
Beginner tip
Read the call as “where is otherNode relative to me?” So head.compareDocumentPosition(body) describes body’s position relative to head.
Concept
Understanding compareDocumentPosition()
MDN: the method reports the position of its argument node relative to the node on which it is called.
Bitmask — zero or more Node.DOCUMENT_POSITION_* bits combined.
MDN head/body check and the same-node zero result.
Example 1 — Does body Follow head? (MDN)
Use bitwise AND to test DOCUMENT_POSITION_FOLLOWING.
JavaScript
const head = document.head;
const body = document.body;
if (head.compareDocumentPosition(body) & Node.DOCUMENT_POSITION_FOLLOWING) {
console.log("Well-formed document");
} else {
console.error("<head> is not before <body>");
}
Always interpret flags relative to the receiver. For “is A inside B?” you can also use B.contains(A) when both are Elements—this API is richer for order.
Example 4 — Preceding Sibling
Earlier siblings set DOCUMENT_POSITION_PRECEDING.
JavaScript
const a = document.getElementById("a");
const b = document.getElementById("b");
const mask = b.compareDocumentPosition(a);
console.log(!!(mask & Node.DOCUMENT_POSITION_PRECEDING));
// true — a precedes b relative to b
Node.compareDocumentPosition() 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.compareDocumentPosition()
Safe for production. Remember bitmask & checks; same node returns 0.
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
Full support
compareDocumentPosition()Excellent
Bottom line: Use & with DOCUMENT_POSITION_* flags; do not rely on === against a single flag when bits may combine.
Wrap Up
Conclusion
Node.compareDocumentPosition() describes where another node sits relative to this one as a bitmask. Test with &, remember 0 for the same node, and use the DOCUMENT_POSITION_* constants for clear intent.
Combine checks when order and containment both matter
❌ Don’t
Assume mask === FOLLOWING when other bits may be set
Swap receiver and argument without rethinking the flags
Ignore DISCONNECTED across trees/documents
Confuse DOM Node with the Node.js runtime
Skip & because the raw number “looks right”
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about compareDocumentPosition()
Relative position as a bitmask of document flags.
5
Core concepts
⚖️01
Bitmask
integer flags
API
&02
Use &
test each flag
Check
🔄03
Same = 0
identical node
Edge
📁04
Contains
ancestor / child
Tree
📈05
Bits combine
e.g. 10
Gotcha
❓ 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 a standard DOM method — 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).
Did you know?
MDN’s classic check head.compareDocumentPosition(body) & Node.DOCUMENT_POSITION_FOLLOWING is a tiny way to confirm a well-formed document where <body> follows <head>.