files/en-us/web/api/node/comparedocumentposition/index.md
{{APIRef("DOM")}}
The compareDocumentPosition() method of the {{domxref("Node")}} interface
reports the position of its argument node relative to the node on which it is called.
compareDocumentPosition(otherNode)
otherNode
An integer value representing otherNode's position relative to node
as a bitmask combining the
following constant properties of {{domxref("Node")}}:
Node.DOCUMENT_POSITION_DISCONNECTED (1)
Node.DOCUMENT_POSITION_PRECEDING (2)
otherNode precedes the node in either a pre-order depth-first traversal of a tree containing both (e.g., as an ancestor or previous sibling or a descendant of a previous sibling or previous sibling of an ancestor) or (if they are disconnected) in an arbitrary but consistent ordering.Node.DOCUMENT_POSITION_FOLLOWING (4)
otherNode follows the node in either a pre-order depth-first traversal of a tree containing both (e.g., as a descendant or following sibling or a descendant of a following sibling or following sibling of an ancestor) or (if they are disconnected) in an arbitrary but consistent ordering.Node.DOCUMENT_POSITION_CONTAINS (8)
otherNode is an ancestor of the node.Node.DOCUMENT_POSITION_CONTAINED_BY (16)
otherNode is a descendant of the node.Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC (32)
Zero or more bits can be set, depending on which scenarios apply. For example, if
otherNode is located earlier in the document and
contains the node on which compareDocumentPosition() was
called, then both the DOCUMENT_POSITION_CONTAINS and
DOCUMENT_POSITION_PRECEDING bits would be set, producing a value of 10 (0x0A).
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>");
}
[!NOTE] Because the result returned by
compareDocumentPosition()is a bitmask, the bitwise AND operator must be used for meaningful results.
{{Specifications}}
{{Compat}}