This is the kind of thing Maybe is good for
const maybeSelect = selector => {
const elem = document.querySelector(selector);
return elem ? Just(elem) : Nothing();
};
maybeSelect('#el').effect(x => x.style.display = 'none');
A simplistic (awful) implementation
const Just = (value) => ({
fold: (f = x => x) => f(value),
map: f => Just(f(value)),
effect: f => (f(value), Just(value))
})
const Nothing = () => ({
fold: () => null,
map: f => Nothing(),
effect: f => Nothing()
})
Basically when you use effect on a Nothing, it does nothing, and whether you have a Nothing or a Just is decided in maybeSelect.
map allows you to transform the value and fold to retrieve it.
And of course you can chain effects and maps at will.
It may seem a bit overkill, but once you get used to it, it's addictive.
You can turn to libraries such as Folktale if you are interested.