The Node.contains() method returns whether another node is this node or a descendant of it. Learn the inclusive self-check, null behavior, the MDN isInPage pattern, and how it compares to compareDocumentPosition().
01
Kind
Instance method
02
Returns
boolean
03
Inclusive
Contains itself
04
null
Always false
05
Checks
Descendants
06
Status
Baseline widely
Fundamentals
Introduction
When you need a simple yes/no answer—“is this node inside that container?”— contains() is the clearest API. It walks the descendant relationship, not just direct children.
One beginner surprise: a node contains itself. If you only care about true descendants (or “is this in the page body?”), exclude the self case explicitly, as MDN’s isInPage example does.
💡
Beginner tip
Read parent.contains(child) as “is child inside parent (or equal to it)?” The receiver is the container.
Concept
Understanding contains()
MDN: contains() returns whether a node is a descendant of a given node— itself, a direct child, a grandchild, and so on.
True — otherNode is this node or nested under it.
False — not in the subtree (or otherNode is null).
Inclusive — node.contains(node) is true.
Not only Elements — works with Node; text nodes can be tested too.
Foundation
📝 Syntax
JavaScript
const inside = node.contains(otherNode);
Parameters
otherNode — The node to test. Not optional in the signature, but may be null (then the result is always false).
Return value
A boolean: true if otherNode is contained in the node, otherwise false.
Node.contains() 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.contains()
Safe for production. Remember inclusive self and null → false.
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
contains()Excellent
Bottom line: Use contains for simple inside-subtree checks; use compareDocumentPosition when you also need document order.
Wrap Up
Conclusion
Node.contains() answers whether another node is this node or a descendant. Watch the inclusive self case, treat null as false, and reach for compareDocumentPosition when you need order as well.
It returns true if otherNode is this node or a descendant of this node (child, grandchild, and so on). Otherwise it returns false.
No. MDN marks Node.contains() as Baseline Widely available (since July 2015). It is a standard DOM method — not Deprecated, Experimental, or Non-standard.
Yes. MDN notes that a node is contained inside itself, so node.contains(node) is true.
contains(null) always returns false. The argument is required in the signature but may be set to null.
contains answers a simple yes/no descendant (inclusive) question. compareDocumentPosition returns a bitmask for order, containment, and disconnected cases.
MDN’s isInPage pattern: return false if the node is document.body itself, otherwise return document.body.contains(node).
Did you know?
Click-outside handlers often use !menu.contains(event.target) so a click anywhere outside the menu closes it— including when event.target is a text node inside another element.