the docs say that the nullish operator (??) is the 5th lowest precedence in operation.
I am trying to use them in this manner
const accountNumber = optionsA?.accountNumber ?? optionB?.accountNo ?? '7202705382';
which I hope to mean
accountNumber in descending order precedence:
I am concerned because I want to ensure both that the order is left to right and that the elvis operators in the objects in the first two items do not interfere with the nullish operators valuations or precedence.
I find it helpful to think of it in normal or even slang terms.
optionsA?.accountNumber … ? Are you there? Are you something that isn’t nullish…? If so, you’re now
If not, let’s go knock on
optionB?.accountNo‘s door. If
optionB?.accountNo answers the door (if its not nullish), then IT is now
Otherwise, you gotta settle for the
So is it ok? Yea, it will work. But…
All that being said (and this is totally based on my experience seeing other people’s code over the years in libraries and other codebases where I’ve worked), I strongly recommend not doing this. It can make readability SUPER difficult. In my mind, it’s akin to nested ternaries, which aren’t going to make you the most loved dev in the world.
Answered By – Mytch