document.evaluate() is an instance method that runs an XPath expression and returns an XPathResult (see MDN Document: evaluate()). Learn the five parameters, common result types, how to iterate matches, how context nodes improve performance, when to prefer createExpression() or CSS selectors, and five try-it labs.
01
Kind
Instance method
02
Input
XPath string
03
Returns
XPathResult
04
Context
often document
05
Loop with
iterateNext()
06
Status
Baseline
Fundamentals
Introduction
XPath is a query language for trees. With document.evaluate() you pass an XPath string plus a context node, and the browser returns an XPathResult — often a node iterator you walk with iterateNext().
MDN: XPath works on both HTML and XML documents. For everyday element picks, querySelector / querySelectorAll are usually clearer. Reach for evaluate() when you need XPath features (for example count(), axis-style paths, or XML-oriented queries).
💡
Think: CSS selectors’ powerful cousin
1) Write an XPath string 2) Pick a context node (often document) 3) Choose a result type (or use ANY_TYPE) 4) Read nodes with iterateNext() or value fields
xpathExpression — a string representing the XPath to evaluate (MDN).
contextNode — the context node for the query. It is common to pass document (MDN).
namespaceResolver(optional) — a function that receives a namespace prefix and returns its URI. Use null for HTML or when no prefixes appear. Defaults to null if omitted (MDN).
resultType(optional) — integer for the XPathResult type. Defaults to ANY_TYPE (0) (MDN).
result(optional) — an existing XPathResult to fill, or null / omitted to create a new one (MDN).
Return value
An XPathResult. If result was null (or omitted), it is a new object; otherwise it is the same object you passed in (MDN).
Common resultType values (MDN)
Constant
Value
Use for
ANY_TYPE
0
Whatever type the expression naturally produces (default)
NUMBER_TYPE
1
A single number (e.g. count())
STRING_TYPE
2
A single string
BOOLEAN_TYPE
3
A single boolean (e.g. not())
UNORDERED_NODE_ITERATOR_TYPE
4
All matching nodes; order not guaranteed
ORDERED_NODE_ITERATOR_TYPE
5
All matching nodes in document order
UNORDERED_NODE_SNAPSHOT_TYPE
6
Snapshot list; order not guaranteed
ORDERED_NODE_SNAPSHOT_TYPE
7
Snapshot list in document order
ANY_UNORDERED_NODE_TYPE
8
Any single matching node
FIRST_ORDERED_NODE_TYPE
9
First matching node in document order
MDN notes for iterators: they hold live references. Modifying the document can invalidate the iterator — iterating afterward may throw. Snapshots are not invalidated by DOM changes, but they may no longer match the live tree.
MDN example (find all h2 headings)
JavaScript
const headings = document.evaluate(
"/html/body//h2",
document,
null,
XPathResult.ANY_TYPE,
null,
);
let thisHeading = headings.iterateNext();
let alertText = "Level 2 headings in this document are:\n";
while (thisHeading) {
alertText += `${thisHeading.textContent}\n`;
thisHeading = headings.iterateNext();
}
alert(alertText);
Snapshots behave like a fixed list of matched nodes. MDN: changing the document does not invalidate the snapshot, but the list may become out of date relative to the live tree.
Applications
🚀 Common Use Cases
One-off XPath queries — run a path without compiling an expression first.
XML / mixed documents — namespace-aware selection with a resolver.
Numeric / boolean XPath — count(), not(), and similar.
Scoped searches — pass a panel or document.body as context.
Legacy tooling — scripts and tests that already speak XPath.
Not every UI query — prefer CSS selectors for simple HTML picks.
🧠 How evaluate() Works
1
Pass XPath + context
MDN: provide the expression string and a context node (often document).
Input
2
Resolver + result type
Use null for HTML namespaces; pick ANY_TYPE or an explicit type.
Options
3
Get XPathResult
A new result object (or the one you reused via the result parameter).
Run
4
✓
Read matches or values
Iterate nodes, use singleNodeValue, or read numberValue / string / boolean.
Important
📝 Notes
MDN: Baseline Widely available since July 2015.
MDN: works on both HTML and XML documents.
MDN: pass null for namespaceResolver on typical HTML pages.
MDN: prefer specific paths and tight contexts over a bare // on large trees.
Do not mutate the DOM while walking an iterator without understanding invalidation (MDN).
Document.evaluate() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.evaluate()
Run XPath expressions against HTML and XML documents across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
evaluate()Wide
Bottom line: Use document.evaluate for one-shot XPath. Prefer createExpression for reuse, and querySelector for simple HTML selection.
Wrap Up
Conclusion
document.evaluate(xpath, context, null, type, null) runs an XPath query and returns an XPathResult. Use iterators for a one-pass walk, snapshots when you need indexed access, and scalar types for count()-style expressions — exactly the toolkit MDN documents.
Pass null for the namespace resolver on plain HTML (MDN)
Use a tighter contextNode and relative .// paths (MDN)
Pick an explicit result type when you know you need nodes vs numbers
Prefer snapshots if you will mutate the DOM while reading matches
Compile with createExpression when the same path runs many times
❌ Don’t
Reach for XPath when a simple CSS selector is enough
Rely on a bare // path on huge documents without considering cost (MDN)
Mutate the tree while iterating without knowing invalidation rules (MDN)
Forget singleNodeValue can be null when nothing matches
Skip a namespace resolver when your XPath uses prefixes
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about evaluate()
Run XPath once; read an XPathResult.
5
Core concepts
📝01
Returns
XPathResult
MDN
🔄02
Loop
iterateNext()
nodes
📄03
Context
often document
or body
⚡04
Reuse?
createExpression
compile
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.evaluate() selects nodes (or other values) based on an XPath expression. It works on both HTML and XML documents.
No. MDN marks Document.evaluate() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
An XPathResult. If you pass null (or omit) for the result parameter, you get a new XPathResult. If you pass an existing XPathResult, that same object is reused and returned (MDN).
MDN: null is common for HTML documents or when no namespace prefixes are used. If omitted, it defaults to null.
Use document.createExpression() when you will run the same XPath many times. For a one-off query, document.evaluate() is often enough.
MDN notes that more specific paths (for example /html/body//h2) and a tighter contextNode often perform better than a bare // shortcut on large documents.
Did you know?
MDN’s xml:id helper uses document.evaluate with a tiny namespace resolver that always returns the XML namespace URI — a handy pattern when attribute names carry a prefix and CSS selectors are not enough.