Element.classList is a read-only instance property that returns a live DOMTokenList of the element’s CSS classes. Learn add, remove, toggle, contains, and replace—and why it beats string surgery on className—with five examples and try-it labs.
01
Kind
Instance property
02
Access
Read-only list
03
Type
DOMTokenList
04
Reflects
class attribute
05
Status
Baseline widely
06
Key APIs
add · toggle · remove
Fundamentals
Introduction
Almost every interactive UI toggles CSS classes: open menus, active tabs, dark mode, loading spinners. Working with className as one big string is error-prone.
classList gives you a clean token API: add one class, remove another, or toggle a state without worrying about extra spaces or duplicate names.
Prefer classList for day-to-day class changes. Use className when you intentionally want to replace the entire class string at once.
Concept
Understanding the Property
MDN: the read-only classList property of the Element interface contains a live DOMTokenList collection representing the class attribute of the element. This can then be used to manipulate the class list.
Live token list — mirrors the current class attribute.
Property is read-only — mutate tokens with methods, not by replacing the object.
Safer than string edits — no manual space handling.
Baseline — widely available since October 2017 (MDN).
Foundation
📝 Syntax
JavaScript
classList
Value
A DOMTokenList representing the element’s class attribute. If class is missing or empty, you get an empty list (length === 0).
Item
Detail
Type
DOMTokenList
Property
Read-only (methods mutate the tokens)
Common methods
add, remove, toggle, contains, replace
vs className
className is one string; classList is a token API
⚠️
Assigning a string
el.classList = "foo bar" is forwarded to classList.value. Prefer explicit add / remove for clarity in tutorials and apps.
Pattern
📋 MDN classList Example Shape
MDN starts with a div, then uses the classList API to remove, add, toggle, and check classes:
Element.classList is Baseline Widely available (MDN: across browsers since October 2017). Logos use the shared browser-image-sprite.png sprite from this project.
Bottom line: Use classList for everyday class changes. Prefer toggle(class, force) for boolean UI state. Avoid fragile string concatenation on className.
Wrap Up
Conclusion
classList is the modern way to manage CSS classes in JavaScript. Use add, remove, and toggle for UI state, and leave messy string edits behind.
Add/remove multiple classes in one call when needed
Check with contains before branching
Keep class names semantic (is-open, is-active)
Prefer classList over string surgery
❌ Don’t
Concatenate spaces onto className by hand
Assume assigning to classList replaces the object
Use invalid token characters in class names
Forget that toggle without force flips state
Rely on IE’s incomplete older classList support
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about classList
Live DOMTokenList for CSS classes—add, remove, toggle, and replace safely.
5
Core concepts
📄01
Get only
on Element
Kind
📝02
DOMTokenList
live tokens
Type
🔍03
UI state
menus & themes
Use
✅04
Baseline
widely available
Status
🎯05
toggle
best for boolean
Tip
❓ Frequently Asked Questions
It returns a live DOMTokenList of the element's CSS classes from the class attribute, with helpers like add, remove, toggle, contains, and replace.
No. MDN marks Element.classList as Baseline Widely available (since October 2017). It is not Deprecated, Experimental, or Non-standard.
The property itself is read-only (you cannot replace the DOMTokenList object), but you can assign a string to classList (forwarded to value) and you can mutate tokens with add/remove/toggle/replace.
className is a single space-separated string. classList is a token list API that makes adding, removing, and toggling individual classes safer and clearer.
Yes. Pass multiple arguments: el.classList.add("foo", "bar"), or use spread: el.classList.add(...array).
el.classList.toggle("visible", condition) adds the class when the condition is true and removes it when false.
Did you know?
DOMTokenList is iterable—so you can use for (const name of el.classList) to walk every class token.