classList.toggle()

Widely available Reference ↗

classList.toggle() accepts a second boolean argument. That turns a common add-or-remove branch into one explicit line.

document.body.classList.toggle(
  'menu-open',
  checkbox.checked,
);  

Without the second argument, toggle() flips the current state. With it, the resulting state follows the boolean, which is usually safer for UI synchronization.

Originally spotted in a tweet by David DeSandro.