DOM Methods
DOMTokenList.contains()
Checks exact token membership, not substring containment. For classList, contains("active") does not match "inactive" or "active-card". Match a single class name with its exact case; pass separate tokens to add or remove rather than a space-separated phrase. Example result: false.
Formal Syntax & Structure
html syntaxhtml
list.contains(token)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| token | string | Nonempty token without ASCII whitespace. |
Return Value
boolean
True only when the exact token is present.
Working Example
Example Snippethtml
<p> Result: <output id="result"></output></p><script> const result = document.getElementById("result"); (async () => { const el = document.createElement("div"); el.className = "card active"; const list = el.classList; result.textContent = list.contains("act"); })().catch(error => { result.textContent = "Error: " + error.name; });</script>