Manage CSS class tokens
Quick answer
classList exposes individual class tokens. add and remove change selected tokens; toggle accepts an optional force boolean to make the desired state explicit.
Browser example
Run in a browser document, not a bare Node.js process.
const e=document.createElement("div");e.classList.add("card");e.classList.toggle("active",true);e.classList.toggle("active",true);console.log(e.className);Expected console output:
card activeCommon mistake
A class token must not contain ASCII whitespace.
Practice
Set a selected class from a Boolean twice and verify idempotence.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Manage CSS class tokens — Knowledge Check
Check the central concept, apply it to a new scenario, and predict browser behavior.
Which call ensures active is present even after repeated calls?
You need to add selected without removing the existing card class. Which operation fits?
What does toggling active with force false do if active is already absent?
Why pass card and active as separate tokens rather than 'card active' as one token to add?
What is printed by this browser snippet? (Manage CSS class tokens)
const e=document.createElement("div");e.classList.add("card","active");e.classList.toggle("active",false);e.classList.add("new");console.log(e.className);