As you've found, this can be done with custom properties, and assuming browser compatibility is not an issue (I don't know if any of the browsers, particularly Edge and Safari, have issues with custom properties on pseudo-elements but it's worth a try), custom properties are the ideal way of customizing pseudo-element styles with JavaScript, as a workaround for the CSSOM currently only supporting reading pseudo-element styles but not writing them.
If you're worried about browser compatibility, you'd have to do it the old-fashioned ways, as described here:
You can always find other ways around it, though, for example:
Applying the styles to the pseudo-elements of one or more arbitrary classes, then toggling between classes (see seucolega's answer for a quick example) — this is the idiomatic way as it makes use of simple selectors (which pseudo-elements are not) to distinguish between elements and element states, the way they're intended to be used
Manipulating the styles being applied to said pseudo-elements, by altering the document stylesheet, which is much more of a hack
... with more examples given by other answers to the same question.