JavaScript Element classList Property

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance property

What You’ll Learn

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

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.

JavaScript
const btn = document.querySelector("button");
btn.classList.add("is-active");
btn.classList.toggle("is-loading");
console.log(btn.classList.contains("is-active")); // true
💡
Beginner tip

Prefer classList for day-to-day class changes. Use className when you intentionally want to replace the entire class string at once.

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).

📝 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).

ItemDetail
TypeDOMTokenList
PropertyRead-only (methods mutate the tokens)
Common methodsadd, remove, toggle, contains, replace
vs classNameclassName 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.

📋 MDN classList Example Shape

MDN starts with a div, then uses the classList API to remove, add, toggle, and check classes:

JavaScript
const div = document.createElement("div");
div.classList = "foo"; // forwarded to classList.value
console.log(div.outerHTML); // <div class="foo"></div>

div.classList.remove("foo");
div.classList.add("another-class");
console.log(div.outerHTML); // <div class="another-class"></div>

div.classList.toggle("visible");
console.log(div.classList.contains("foo")); // false

div.classList.add("foo", "bar", "baz");
div.classList.replace("foo", "qux");

Related learning: className, CSS class selectors, and UI state patterns like is-open / is-active.

⚡ Quick Reference

GoalCode / note
Addel.classList.add("active")
Removeel.classList.remove("active")
Toggleel.classList.toggle("open")
Force toggleel.classList.toggle("open", isOpen)
Checkel.classList.contains("active")
Replaceel.classList.replace("old", "new")
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.classList.

Kind
get only

Instance

Type
DOMTokenList

Live tokens

Mutate via
methods

add / toggle

Baseline
widely

Oct 2017+

Examples Gallery

Examples follow MDN Element: classList. Labs create a simple div so you can safely call the token methods.

📚 Getting Started

Inspect the list, then add and remove classes.

Example 1 — Read classList

Log length, value, and whether a class is present.

JavaScript
const div = document.createElement("div");
div.className = "card featured";
console.log(div.classList.length);
console.log(div.classList.value);
console.log(div.classList.contains("card"));
Try It Yourself

How It Works

Each space-separated class becomes one token. value is the serialized string (same idea as className).

Example 2 — add and remove

MDN pattern: swap classes without rewriting the whole string.

JavaScript
const div = document.createElement("div");
div.classList = "foo";
div.classList.remove("foo");
div.classList.add("another-class");
console.log(div.outerHTML);
Try It Yourself

How It Works

add and remove accept multiple names: add("a", "b"). Duplicates are ignored by add.

📈 Toggle, Replace & Snapshot

Practice UI-state toggles and class replacement.

Example 3 — toggle and Force Flag

Flip a class, then force it on or off with a boolean.

JavaScript
const div = document.createElement("div");
div.classList.toggle("visible"); // adds
console.log(div.classList.contains("visible"));

div.classList.toggle("visible"); // removes
console.log(div.classList.contains("visible"));

const i = 5;
div.classList.toggle("visible", i < 10); // force add
console.log(div.classList.value);
Try It Yourself

How It Works

One-argument toggle flips presence. Two-argument form is perfect for “set state from a boolean.”

Example 4 — replace and Multi-Add

Swap one class for another and add several tokens at once.

JavaScript
const div = document.createElement("div");
div.classList.add("foo", "bar", "baz");
div.classList.replace("foo", "qux");
console.log(div.classList.value);

const cls = ["alpha", "beta"];
div.classList.add(...cls);
console.log(div.classList.contains("alpha"));
Try It Yourself

How It Works

replace returns true when the old token existed. Spread syntax keeps class arrays tidy.

Example 5 — Support Snapshot

Feature-detect and remember the core methods.

JavaScript
console.log({
  supported: "classList" in Element.prototype,
  type: "DOMTokenList",
  tip: "Prefer classList over string edits on className",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Modern browsers all expose classList. Use it as the default class API in new code.

🚀 Common Use Cases

  • Opening and closing menus, modals, and drawers.
  • Marking the active tab, nav link, or selected card.
  • Toggling dark mode or theme classes on <html> / <body>.
  • Showing loading / error / success states on buttons and forms.
  • Replacing a temporary class after an animation ends.

🔧 How It Works

1

class attribute holds tokens

Space-separated names like card featured.

Markup
2

classList exposes DOMTokenList

A live view of those individual tokens.

List
3

Methods update tokens

add, remove, toggle, replace.

Mutate
4

CSS styles update

The attribute and styles stay in sync automatically.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • The classList property is read-only; mutate tokens with methods.
  • Safer and clearer than manually editing className strings.
  • Related: children, className, EventTarget, JavaScript hub.

Browser Support

Element.classList is Baseline Widely available (MDN: across browsers since October 2017). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.classList

Read-only DOMTokenList — add, remove, toggle, contains, and replace CSS classes.

Baseline Widely available
Google Chrome Supported (DOMTokenList)
Yes
Microsoft Edge Supported (DOMTokenList)
Yes
Mozilla Firefox Supported (DOMTokenList)
Yes
Apple Safari Supported (DOMTokenList)
Yes
Opera Supported (DOMTokenList)
Yes
Internet Explorer Partial (IE10+; limited methods)
Partial
classList Baseline

Bottom line: Use classList for everyday class changes. Prefer toggle(class, force) for boolean UI state. Avoid fragile string concatenation on className.

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.

Continue with className, children, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use toggle(name, force) for boolean UI state
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about classList

Live DOMTokenList for CSS classes—add, remove, toggle, and replace safely.

5
Core concepts
📝 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.

Next: className

Learn how to get and set the full class attribute string.

className →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful