Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

DOMTokenList.add() — adding tokens

DOMTokenList.add() adds tokens, such as classes through element.classList, skipping duplicates and validating arguments.

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

tokenList.add(...tokens)

DOMTokenList.add() adds tokens to an ordered set, for example through element.classList. It preserves existing tokens, skips duplicates, and appends new tokens in argument order. Calling add() with no arguments is valid. The method updates the associated attribute unless the token set is empty and the attribute is absent. In that case, the attribute remains absent.

Pass separate strings: element.classList.add('card', 'active'). The method returns undefined, not the list or a boolean.

An empty token throws a DOMException named SyntaxError; ASCII whitespace within a token throws InvalidCharacterError. All tokens are validated before insertion: an invalid argument prevents even valid tokens in that call from being added.

This is a DOM API, not an ECMAScript-only feature. The example requires a browser document; the presence of Node 24 does not establish support for this API.

Examples

const element = document.createElement('div');
element.classList.add('card');
const result = element.classList.add('card', 'active');
console.log(element.className);
console.log(result);

try {
  element.classList.add('pending', 'not valid');
} catch (error) {
  console.log(error.name);
}
console.log(element.className);

Result

card active
undefined
InvalidCharacterError
card active

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