Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

DOMTokenList.contains — checking token membership

DOMTokenList.contains(token) reports whether a list contains the exact token, for example a CSS class in element.classList.

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.contains(token)

DOMTokenList.contains(token) returns true when the list contains the supplied token, otherwise false. It does not modify the list. A typical use is element.classList.contains('active').

It checks a whole token, not a substring or CSS selector. Matching is case-sensitive: active and Active are different tokens. Do not prefix a class name with a dot.

An empty string or a string containing ASCII whitespace returns false, without the validation exceptions associated with add(). This is a DOM Web API, not ECMAScript; the example requires a browser DOM, which Node 24 alone does not provide.

Examples

const element = document.createElement('div');
element.className = 'active item';
const tokens = element.classList;

console.log(tokens.contains('active'));
console.log(tokens.contains('act'));
console.log(tokens.contains('Active'));
console.log(tokens.contains('active item'));
console.log(tokens.contains(''));

Result

true
false
false
false
false

The list contains two separate tokens: `active` and `item`. Only the first check returns `true`. `tokens.contains('active item')` checks the entire string as one token and returns `false` rather than throwing a `SyntaxError`, so the final check still runs and returns `false`.

Sources