Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

DOMTokenList — managing tokens through classList

DOMTokenList represents a set of tokens, such as an element's classes exposed through classList. Learn to add, remove, toggle, and check tokens without manually processing the text of the class attribute.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const tokens = element.classList;
tokens.add(token1, token2);
tokens.remove(token1, token2);
tokens.toggle(token);
tokens.toggle(token, force);
tokens.contains(token);

DOMTokenList represents an ordered set of tokens and is case-sensitive. Only ASCII whitespace separates tokens in the attribute value. For example, a nonbreaking space (U+00A0) can be part of a single class token rather than separating two classes. A common example is element.classList: a live object reflecting the class attribute. List changes update the attribute, and attribute changes are visible through the same object. It is not a JavaScript array.

DOMTokenList.add, called as tokens.add("card", "active"), adds individual tokens and skips those already present. DOMTokenList.remove, for example tokens.remove("active", "missing"), removes the specified tokens; a missing token causes no error. Both methods accept multiple arguments and return undefined. Pass classes separately: tokens.add("card", "active"), not tokens.add("card active"). For add(), an empty token causes a DOMException named SyntaxError, while a token containing ASCII whitespace causes InvalidCharacterError.

DOMTokenList.contains, used as tokens.contains("active"), returns true or false without changing the list. DOMTokenList.toggle, used as tokens.toggle("selected"), adds an absent token or removes a present one. Its return value reports membership after the operation, not whether a change occurred. The tokens.toggle("ready", force) form with boolean true ensures presence, while false ensures absence. Use this form when synchronizing with an explicit state instead of blindly toggling.

This is a DOM API, not a feature of a particular ECMAScript edition. MDN marks the interface and the four methods discussed here as widely available since July 2015; check support for specific call variants when targeting older browsers. Having Node 24 in a repository does not establish DOM support. The example requires a browser; the element need not be attached to the document.

Examples

{
  const element = document.createElement("div");
  const tokens = element.classList;

  tokens.add("card", "active", "card");
  console.log(tokens.contains("active"));

  tokens.remove("active", "missing");
  console.log(tokens.toggle("selected"));
  console.log(tokens.toggle("selected", false));

  tokens.toggle("ready", true);
  console.log(element.className);
}

Result

true
true
false
card ready

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources