classList.remove should work if no class is assigned.

let domElement = document.getElementById(domElement);
domElement.classList.remove(I don't exist);

The browser will throw an error: "Cannot read properties of undefined (reading 'remove')".

But it does not make any sense. If I need to remove some class name then why should I check if it exists? I want to remove it anyway.

a.) This forum is for JavaScript, and the thing you're asking about is in the DOM spec, which is a different thing.

b.) The error you're getting indicates that the classList property doesn't exist at all, possibly because you're on an old browser or have a typo or something. Removing a class which isn't present does in fact work fine.

4 Likes