Element.matches() is an instance method that tests whether the element would be selected by a CSS selector string. It returns true or false—perfect for filtering lists, conditional styling logic, and loops. Learn the MDN birds example, comparison with closest(), and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Tests
Current element
03
Arg
CSS selector string
04
Returns
true / false
05
Throws
SyntaxError
06
Status
Baseline widely
Fundamentals
Introduction
When you loop through a list of elements, you often need a quick yes/no answer: “Does this node match my CSS rule?” Before matches(), developers checked className strings or wrote custom attribute tests.
matches(selectors) delegates that work to the browser’s CSS engine. Pass any valid selector—class, tag, ID, attribute, or pseudo-class—and get a boolean back. MDN: the method tests whether the element would be selected by the specified CSS selector.
💡
Beginner tip
matches() only checks the current element. To find a matching ancestor, use closest() instead.
Calling el.matches(selectors) asks the browser whether el would match if you ran that selector against the document.
It is an instance method on Element.
Tests only the element you call it on—no ancestor walk (MDN).
Returns true if the element matches, otherwise false (MDN).
Accepts the same selector strings as querySelector and querySelectorAll.
Throws SyntaxError if selectors is not valid CSS (MDN).
Formerly known as matchesSelector() in older drafts; also had vendor prefixes (webkitMatchesSelector, etc.).
Foundation
📝 Syntax
General form of Element.matches (MDN):
JavaScript
matches(selectors)
Parameters
Name
Type
Description
selectors
String
A string containing valid CSS selectors to test the element against (MDN).
Return value
Type
Description
Boolean
true if the element matches the selectors; otherwise false (MDN).
Exceptions
SyntaxErrorDOMException — thrown if selectors cannot be parsed as a valid CSS selector list (MDN).
Common patterns
JavaScript
// MDN birds example
for (const bird of document.querySelectorAll("li")) {
if (bird.matches(".endangered")) {
console.log(`The ${bird.textContent} is endangered!`);
}
}
// Class check
if (card.matches(".active")) { /* ... */ }
// Compound selector
if (btn.matches("button.primary:enabled")) { /* ... */ }
// Attribute selector
if (link.matches("[href^='https']")) { /* ... */ }
Cheat Sheet
⚡ Quick Reference
Goal
Code
Test class on element
el.matches(".active")
Test tag name
el.matches("li")
Compound selector
el.matches("li.endangered")
Filter in a loop
if (item.matches(sel)) { ... }
No match
false
MDN status
Baseline Widely available (since April 2017)
Snapshot
🔍 At a Glance
Four facts to remember about Element.matches().
Returns
boolean
true or false
Baseline
widely
Since April 2017
Scope
self
Current element only
Input
CSS
Selector string
Compare
📋 matches() vs closest() vs classList.contains()
el.matches(sel)
el.closest(sel)
el.classList.contains("x")
Scope
Current element only
Self + ancestors
Single class token
Return value
true / false
Element or null
true / false
Selector power
Full CSS selectors
Full CSS selectors
Class name only
Loop filtering
Ideal
Overkill
Limited
Best for
Test current node
Find nearest container
Simple class check
Hands-On
Examples Gallery
Examples follow MDN Element.matches() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
MDN’s endangered birds list—filter with matches().
Example 1 — Endangered Birds (MDN)
Loop list items and log only those matching .endangered.
JavaScript
const birds = document.querySelectorAll("li");
for (const bird of birds) {
if (bird.matches(".endangered")) {
console.log(`The ${bird.textContent} is endangered!`);
}
}
// "The Philippine eagle is endangered!"
matches(".active") returns true only when the element would be selected by that class selector. For a single class, it works like classList.contains("active") but accepts full CSS syntax.
📈 Practical Patterns
Compound selectors, pseudo-classes, and batch filtering.
Element.matches() is Baseline Widely available (MDN: across browsers since April 2017). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.matches()
Test whether an element matches a CSS selector — returns true or false.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet Explorer9+ · Unprefixed in IE9+
Yes
matches()Excellent
Bottom line: Use matches() to test the current element against any CSS selector. Pair with loops for filtering, and use closest() when you need ancestors.
Wrap Up
Conclusion
Element.matches() is the standard way to ask “does this element match my CSS selector?” It returns true or false, making it ideal for loops, filters, and conditional UI logic.
Use in loops to filter querySelectorAll results (MDN)
Prefer expressive CSS over manual property checks
Use classList.contains() for a single class; matches() for complex selectors
Combine with pseudo-classes: :enabled, :invalid, :not()
Wrap dynamic selectors in try/catch if user-provided
❌ Don’t
Use matches() when you need an ancestor (use closest)
Build selectors from unescaped user input
Expect it to search child elements
Confuse return value with an Element reference
Rely on vendor-prefixed aliases in new code
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.matches()
Boolean CSS selector tests on the current element.
5
Core concepts
📝01
Returns
true | false
boolean
📄02
Scope
self only
Element
✍️03
Arg
CSS selector
Input
✅04
Baseline
widely available
Status
⚡05
Pattern
loop filter
MDN
❓ Frequently Asked Questions
It tests whether the element would be selected by the given CSS selector string (MDN). It returns true if the element matches, otherwise false.
No. MDN marks Element.matches() as Baseline Widely available (across browsers since April 2017). It is not Deprecated, Experimental, or Non-standard.
A boolean: true if the element matches the selectors, false if it does not (MDN).
matches() tests only the current element. closest() walks up through ancestors (including self) and returns the first matching Element or null.
Yes. matches() accepts the same valid CSS selector strings as querySelector and querySelectorAll (MDN).
A SyntaxError DOMException is thrown if selectors cannot be parsed as a valid CSS selector list (MDN).
Did you know?
MDN’s birds example shows why matches() shines in loops: you can express “is this an endangered bird?” as bird.matches(".endangered") instead of parsing className manually.