document.querySelector() is an instance method that returns the firstElement matching a CSS selector string (see MDN Document: querySelector()). Learn simple class lookups, complex and :not() selectors, null / SyntaxError handling, escaping special ids with CSS.escape(), how it compares to getElementById() and querySelectorAll(), and five try-it labs.
01
Kind
Instance method
02
Arg
CSS selectors
03
Returns
Element | null
04
Match
First only
05
Throws
SyntaxError
06
Status
Baseline
Fundamentals
Introduction
If you already write CSS, you already know the language of querySelector. Pass the same selector string you would use in a stylesheet, and JavaScript hands you the first matching element in the document.
MDN: matching uses depth-first pre-order traversal starting from the first element in the markup. If an id is duplicated, the first element with that id wins. CSS pseudo-elements never return elements.
💡
Think: “CSS find — first hit only”
1) Write a valid CSS selector (e.g. ".card" or "#app") 2) Call document.querySelector(selector) 3) Check for null before using the element 4) Use querySelectorAll when you need every match
Return value — first matching Element, or null (MDN).
Order — depth-first pre-order through the document tree (MDN).
Duplicate ids — the first id match is returned (MDN).
Pseudo-elements — never return any elements (MDN).
Escape special ids — use CSS.escape() when an id is not a valid CSS identifier (MDN).
Foundation
📝 Syntax
General form of Document.querySelector (MDN):
JavaScript
querySelector(selectors)
Parameters
selectors — a string containing one or more selectors to match. Must be valid CSS; otherwise a SyntaxError is thrown (MDN). Escape class/id values that are not valid CSS identifiers with CSS.escape() (MDN).
Return value
An Element representing the first match, or null if there are no matches (MDN). For all matches, use querySelectorAll() instead (MDN).
Exceptions
SyntaxErrorDOMException — thrown if the selector syntax is invalid (MDN).
Common beginner patterns
JavaScript
document.querySelector(".myclass");
document.querySelector("#app");
document.querySelector("div.user-panel.main input[name='login']");
document.querySelector("div.user-panel:not(.main) input[name='login']");
const el = document.querySelector(".card");
if (el) {
el.classList.add("is-active");
}
Compare
⚖️ querySelector vs getElementById vs querySelectorAll
Document.querySelector() is Baseline Widely available on MDN (across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.querySelector()
CSS-powered first-match DOM lookup — Element or null across all major browsers.
BaselineWidely available
Google Chrome1+
Yes
Mozilla Firefox3.5+
Yes
Apple Safari3.1+
Yes
Microsoft Edge12+
Yes
Opera10+
Yes
Internet Explorer8+
Yes
querySelector()Wide
Bottom line: Use querySelector for flexible CSS lookups. Prefer getElementById for simple unique ids, and querySelectorAll when you need every match.
Wrap Up
Conclusion
document.querySelector(selectors) is the everyday way to grab the first element that matches a CSS selector. Check for null, wrap invalid selectors carefully, escape special ids with CSS.escape(), and switch to querySelectorAll when you need every match.
Use querySelectorAll when you need a full list (MDN)
❌ Don’t
Assume a match always exists
Pass invalid CSS without try/catch
Expect CSS pseudo-elements to return nodes (MDN)
Forget that only the first match is returned
Duplicate ids and rely on “whichever one you meant”
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about querySelector()
CSS-powered first-match lookup — Element or null.
5
Core concepts
📝01
Returns
Element | null
MDN
🔎02
Match
first only
CSS
⚠️03
Invalid
SyntaxError
MDN
🛡04
Special ids
CSS.escape
MDN
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.querySelector() returns the first Element within the document that matches the specified CSS selector (or group of selectors). If no matches are found, it returns null.
No. MDN marks Document.querySelector() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
The method returns null (MDN). Always check for null before reading properties like textContent or style.
MDN: a SyntaxError DOMException is thrown if the selectors string is not valid CSS.
querySelector() returns the first matching Element (or null). querySelectorAll() returns a static NodeList of all matches. Use querySelectorAll when you need every match.
When you have a unique, simple id and want the classic fast path. Use querySelector when you need classes, attributes, combinators, or :not() — and remember to escape special characters in ids with CSS.escape().
Did you know?
The same ParentNode method exists on elements as Element.querySelector(). Calling it on a container searches inside that element, which is often faster and clearer than starting from document with a long path.