Check if an element contains a class in JavaScript?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The standard way to check whether a DOM element has a CSS class in modern JavaScript is element.classList.contains("className"). It is safer and clearer than manually parsing the className string because it works with actual class tokens rather than raw text fragments.
This matters because class names are a token list, not a free-form string. Once you treat them as tokens, false matches and fragile string parsing mostly disappear.
Use classList.contains()
Every modern browser exposes classList on ordinary DOM elements. The contains() method returns true only if the class exists as an actual token.
This is the recommended answer for almost all normal DOM code.
Why It Is Better Than className
Older code often manipulates element.className directly. That can work, but it is easier to get wrong.
Bad pattern:
This can incorrectly match inactive, active-item, or other unintended substrings.
A token-aware API avoids that entire class of bug.
A slightly better fallback is splitting the string manually:
But classList.contains() is still clearer and less error-prone.
A Simple Real Example
This is a common pattern in menus, tabs, validation states, and interactive components.
Be Careful with Event Targets
If you get the node from an event, make sure it is actually an element before reading classList.
That extra check helps when event bubbling or complex DOM content makes the target less predictable than you expected.
Checking Many Elements
If you need to inspect several elements, query them first and then loop over them.
Remember that a NodeList is a collection, not a single element. classList.contains() belongs on each element, not on the entire list.
classList Is Useful Beyond Checking
One reason classList is the right API is that it keeps the whole workflow consistent. The same interface lets you inspect and modify classes.
That is easier to maintain than mixing manual string inspection with token-based updates.
Older Environment Fallback
If you ever have to support very old browser environments where classList is not available, you can fall back to splitting className.
Treat that as a compatibility fallback, not the preferred modern solution.
Common Pitfalls
The biggest mistake is using substring matching on className, which can return false positives. Another is calling classList.contains() on something that is not actually a DOM element, such as an unexpected event target. Developers also often forget that querySelectorAll() returns a collection rather than one element. Finally, manual string parsing makes whitespace and token-boundary bugs much easier to introduce than simply using classList directly.
Summary
- Use
element.classList.contains("name")to check for a class. - It is more reliable than parsing
classNamemanually. - Avoid substring searches such as
indexOfon the full class string. - Validate event targets when needed before reading
classList. - Use the rest of the
classListAPI for add, remove, and toggle operations too.

