Almost every interactive widget switches classes: a menu gets open, a tab gets selected, a field gets invalid. element.classList does this for you, and knowing what it does underneath (how it reads the attribute, why it never adds a class twice, when it throws) is a common interview task and the quickest way to understand it.
Write four helpers in classes.js that work on the class attribute itself, without classList.
The names of an element are its class attribute split on any run of ASCII whitespace (space, tab, line feed, form feed, carriage return). A missing attribute means no names.
hasClass matches whole names exactly (case-sensitive). It never throws: for "" or a name containing a space it returns false.
addClass adds each name that is not there yet, at the end, in the order given.
removeClass removes every copy of each name given and keeps the others in order. A name that is not there is skipped.
When the helpers write, the attribute is the names joined by single spaces with repeats removed (the first copy stays), as classList writes it.
When the element had no class attribute and no names are left to write, no attribute is created. When it had one, it is written even if it is now empty (class="").
toggleClass removes the name if present and adds it if missing, and returns whether the element has it afterwards. With forcetrue it only adds; with forcefalse it only removes.
In addClass, removeClass and toggleClass, an empty name throws a DOMException named "SyntaxError", and a name containing whitespace throws one named "InvalidCharacterError". Every name is checked before anything changes.
The helpers work on SVG elements as well as HTML elements.
addClass(tab, "ok", "two words"); // throws InvalidCharacterError; "ok" is not addedremoveClass(document.createElement("p"), "x"); // still no class attribute