Use role="switch" with aria-checked, or visually restyle an <input type="checkbox"> as a switch (its semantics stay checkbox, which screen readers understand natively). Don't signal state with color alone — a nearby "On/Off" label or icon keeps it readable for colorblind users.
Its purpose differs from a checkbox. A checkbox usually represents one of several selections that only take effect after a "Save" button, while a switch applies a single binary setting the instant you press it — Airplane Mode, Dark Mode. That's why a switch conventionally has no adjacent "Save" button.
A switch that writes straight to the server independent of any form submit must roll back to its previous state if that write fails.