DOMTokenList

Element.classList.contains

Returns a boolean indicating whether the element's class list contains the given class

Syntax

JavaScript
element.classList.contains(token)

Parameters

ParameterTypeDescription
tokenstringThe class name to check for

Return Value

true if the class list contains the token, false otherwise

Examples

Basic Usage
const el = document.querySelector('.box')!
console.log(el.classList.contains('active')) // true or false
Practical Example
function isVisible(el: HTMLElement): boolean {
  return !el.classList.contains('hidden')
}
Advanced Usage
const items = document.querySelectorAll('.item')
const selectedCount = Array.from(items).filter(
  el => el.classList.contains('selected')
).length

Understanding Element.classList.contains

The Element.classList.contains method in JavaScript returns a boolean indicating whether the element's class list contains the given class. It belongs to the DOMTokenList object and is one of the most widely used methods for working with domtokenlist values in modern JavaScript and TypeScript applications.

The method signature is element.classList.contains(token). It accepts 1 parameter: token. When called, it returns true if the class list contains the token, false otherwise. Understanding when and how to use contains() helps you write more expressive, readable code.

Common use cases for Element.classList.contains include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-classlist-add, dom-classlist-remove, dom-classlist-toggle, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Element.classList.contains is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More DOMTokenList Methods

Other methods in the DOMTokenList object

Related Tools

More DOMTokenList Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.